问题描述
我在每个 .state
中附加了数据属性来识别用户(经过身份验证的或公开的)如下(一个状态示例)
I have attached data attribute in each .state
to identify the user (authenticated or public) as following (one state example)
$stateProvider
.state('admin-panel.public.home', {
url: '/p',
templateUrl: 'app/public/home.tmpl.html',
controller: 'PublicHomeController',
controllerAs: 'ctrl',
data: {
requireLogin: false
}
});
我需要使用用户(已验证和公开)的一些状态作为示例
I need to use the some state for both of user (authenticated and public) as an example
.state('403', {
url: '/403',
templateUrl: '403.tmpl.html',
controller: function($scope, $state, APP, Auth) {
$scope.app = APP;
$scope.goHome = function() {
if(Auth.isAuthenticated()){
$scope.requireLogin = true;
$state.go('admin-panel.default.home');
}
else{
$scope.requireLogin = false;
$state.go('admin-panel.public.home');
}
};
},
data: {
requireLogin: $scope.requireLogin
}
})
在这里,当经过身份验证的用户访问此状态时,我需要将 true 值传递给 requireLogin: true
以及当公共用户访问此状态时,我需要传递 false
值为 requireLogin: false
.我在 controller
中检查了当前用户状态,如上所述.如何将 $scope.requireLogin
绑定到数据属性?
Here when the authenticated user access this state I need to pass the true value to requireLogin: true
as well when public user access this state I need to pass the false
value as requireLogin: false
. I checked the current user status in the controller
as above. How can I bind the $scope.requireLogin
to data attribute?
ui-router
的高手请教一下解决方法???
Anyone in expert of ui-router
please tell a way to solve???
推荐答案
您可以以更简洁的方式解决您的问题.让我们从一个全局控制器示例开始,GlobalCtrl
被添加到 body
或 html
标签中,如 ng-controller="GlobalCtrl
代码>.
You can solve your problem in a very cleaner way. Let's start with a global controller example GlobalCtrl
which is added to the body
or html
tag like ng-controller="GlobalCtrl
.
这样做将使我们能够在您的单页 Angular 应用程序中保持此 GlobalCtrl
的范围(因为您正在使用 ui-router
),我们可以避免使用$rootScope
的(实际上是模仿了 $rootScope
的用法).
Doing this will enable us to keep the scope of this GlobalCtrl
throughout your single page Angular app (as you are using ui-router
) and we can avoid the usage of $rootScope
(actually mimicking the usage of $rootScope
).
现在,在您的 GlobalCtrl
中定义如下内容:
Now, inside your GlobalCtrl
define something like this:
// Using an object to avoid the scope inheritance problem of Angular
// https://github.com/angular/angular.js/wiki/Understanding-Scopes
$scope.globalData = {};
// Will be called everytime before you start navigating to any state
$scope.$on('$stateChangeStart', function(event, toState, toParams) {
$scope.globalData.requireLogin = false;
var statesToLoginCheck = ['403', 'foo', 'bar']; // and other states on which you want to check if user is logged in or not
// If the current state on which we are navingating is allowed to check for login
if (statesToLoginCheck.indexOf(toState.name) > -1) {
if (Auth.isAuthenticated()) {
$scope.globalData.requireLogin = true;
$state.go('admin-panel.default.home');
} else {
$scope.globalData.requireLogin = false;
$state.go('admin-panel.public.home');
}
event.preventDefault();
return;
}
});
现在,由于 GlobalCtrl
的 $scope
在 body
或 html
中,那么每个状态或指令都将继承此 GlobalCtrl
的范围,然后您只需检查变量 $scope.globalData.requireLogin
的任何控制器.
Now, since $scope
of GlobalCtrl
is in body
or html
then every state or directive will inherit the scope of this GlobalCtrl
and then you simply have to check in your any controller of variable $scope.globalData.requireLogin
.
这篇关于如果在 ui-router 中处于当前状态,如何绑定相关值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!