如何通过当前登录的用户ID来解析ui-router状态的阻塞?(How to pass currently signed in user id to resolve block in ui-router state? (AngularJS))
我正在尝试将当前已登录的用户传递到“用户”状态的解析块(我正在使用ui-router),因此登录后,用户将在... / users /的URL中:ID。
其他路由嵌套在内:用户路由,所以这似乎是我最合乎逻辑的路径(我可能错了)。
我在这里使用angular_devise所以我应该能够通过Auth从API中提取:id,但是我遇到了一些麻烦。
(包括下面其他可能的相关文件。)
App.js
$stateProvder .state('users', { url: '/users/{id}', templateUrl: 'home/_home.html', controller: 'MainCtrl', resolve: { userPromise: ['$stateParams', 'users', 'Auth', function($stateParams, users, Auth) { return users.get( Auth.currentUser().then(function (user) { return user.id; }) ); }] } })
Users.js
.factory('users', ['$http', function($http) { var u = { userArray: [] }; u.get = function(id) { return $http.get('/users/' + id + '.json').then(function(res) { console.log('request: ', res.data); angular.copy(res.data, u.userArray); }); }; return u; }]);
Users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) authorize @user respond_with @user end end
NavCtrl.js
.controller('NavCtrl', [ '$scope', 'Auth', function($scope, Auth) { $scope.signedIn = Auth.isAuthenticated; $scope.logout = Auth.logout; Auth.currentUser().then(function (user) { $scope.user = user; }); $scope.$on('devise:new-registration', function (e, user) { $scope.user = user; }); $scope.$on('devise:login', function (e, user) { $scope.user = user; }); $scope.$on('devise:logout', function (e, user) { $scope.user = {}; }); }]) ;
仅供参考。 $ http请求被发送到rails后端。
如果有人能告诉我可能做错了什么,我将非常感激。
I'm trying to pass the currently signed-in user to the resolve block of a 'user' state (I'm using ui-router), so upon sign-in, user will be in url of .../users/:id.
Other routes are nested inside :user route, so this seems the most logical path to me (I may be wrong).
I'm using angular_devise here so i should be able to extract the :id from from API via Auth, but I'm running into some trouble.
(Included other possible relevant files below.)
App.js
$stateProvder .state('users', { url: '/users/{id}', templateUrl: 'home/_home.html', controller: 'MainCtrl', resolve: { userPromise: ['$stateParams', 'users', 'Auth', function($stateParams, users, Auth) { return users.get( Auth.currentUser().then(function (user) { return user.id; }) ); }] } })
Users.js
.factory('users', ['$http', function($http) { var u = { userArray: [] }; u.get = function(id) { return $http.get('/users/' + id + '.json').then(function(res) { console.log('request: ', res.data); angular.copy(res.data, u.userArray); }); }; return u; }]);
Users_controller.rb
class UsersController < ApplicationController def show @user = User.find(params[:id]) authorize @user respond_with @user end end
NavCtrl.js
.controller('NavCtrl', [ '$scope', 'Auth', function($scope, Auth) { $scope.signedIn = Auth.isAuthenticated; $scope.logout = Auth.logout; Auth.currentUser().then(function (user) { $scope.user = user; }); $scope.$on('devise:new-registration', function (e, user) { $scope.user = user; }); $scope.$on('devise:login', function (e, user) { $scope.user = user; }); $scope.$on('devise:logout', function (e, user) { $scope.user = {}; }); }]) ;
FYI. $http request is being sent to a rails backend.
I would be very grateful if anyone can advise what i might be doing wrong.
原文:https://stackoverflow.com/questions/36011645
最满意答案
您可以尝试在
try
块中进行转换,并忽略catch
块中的异常。 (按照你的建议continue
循环)。 它会工作。另一种方法,不使用异常处理;
还有更多。
You can try to convert in a
try
block, and ignore the exception in thecatch
block. (Justcontinue
the loop, like you suggested). It will work.Another method, without making use of exceptions handling;
Determine if a String is an Integer in Java
How to check if a String is numeric in Java
And there are many more.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
方法arrayList.size() 返回列表中的项目数,所以如果索引大于或等于size() ,它不存在。 The method arrayList.size() returns the number of items in the list - so if the index is greater than or equal to the size(), it doesn't exist. if(index >= myList.size()){ //index not exists }else{ / ...
-
范围[0-Z]是有效的,取决于正则表达式引擎[aZ]将是无效的,或者它将是一个不能匹配任何字符的范围。 在字符类范围中,开始和结束字符只是代码点,并且这些代码点之间的所有字符都将包含在该范围内。 在[0-Z]的情况下,这相当于以下更易读的字符类: [0-9:;<=>?@A-Z] 在[aZ]的情况下,这实际上是一个字符类,它不会匹配任何东西,因为a的代码点比Z更高。 您可以从http://www.asciitable.com/中查看以下ASCII表中的代码点: The range [0-Z] is vali ...
-
您可以尝试在try块中进行转换,并忽略catch块中的异常。 (按照你的建议continue循环)。 它会工作。 另一种方法,不使用异常处理; 确定String是否是Java中的整数 如何检查String中的String是否为数字 还有更多。 You can try to convert in a try block, and ignore the exception in the catch block. (Just continue the loop, like you suggested). It w ...
-
这是有用的东西 preg_match("/[a-z]/i",$text); Here's something that will help preg_match("/[a-z]/i",$text);
-
转换charAt检查'包含AZ'到'不包含AZ'(Converting charAt check 'containing A-Z' to 'not containing A-Z')[2022-04-14]
只是否定你的状况: if (!((name.charAt(i) >= 'a' && name.charAt(i) <= 'z') || ((name.charAt(i) >= 'A' && name.charAt(i) <= 'Z')))) Just negate your condition : if (!((name.charAt(i) >= 'a' && name.charAt(i) <= 'z') || ((name.charAt(i) >= 'A' && name.charAt(i) <= 'Z ... -
几个问题: 你应该使用q.next(); 使用新行字符而不是q.nextInt(); 这基本上是你得到输入不匹配的例外。 您正在执行list.contains(Array) 。 如果您希望检查用户的每个输入是否在Array X中,那么您应该执行以下操作: List
list = Arrays.asList(X); while((ListChecker.hasNext())) { //continues as long as DNA Check has an index to go to ... -
这取决于你所说的“存在”。 如果您的意思是“如果ArrayList 的索引值很高 ”,那么您需要查看Count : If arrayList.Count > yourIndex Then ... End If 如果您的意思是“ArrayList是否在该索引处具有值 ”,只需检查空值: If arrayList(yourIndex) Is Nothing Then ... End If That depends on what you mean by "exists". If you me ...
-
z-index仅适用于定位元素,因此它必须是absolute , relative或fixed 。 只需将的position为position:relative; : input { height: 40px; opacity: 0; z-index: 5; position:relative; } JSFiddle演示 z-index only applies to positioned elements, so it must be absolute, relative, ...