如何在AngularJs的stateProvider状态更改之前加载会话数据? [英] How to load session data before stateProvider state changes in AngularJs?
问题描述
我正在使用angular-acl( https://github.com/mikemclin/angular-acl )来管理我的应用程序的授权逻辑,并且工作正常,除非用户打开新窗口/标签。
I'm using angular-acl (https://github.com/mikemclin/angular-acl) to manage the authorization logic of my application and It's working fine, except when the user open a new window/tab.
当用户打开新窗口时/ tab我无法访问sessionStorage,因此我需要从API重新加载acl和用户角色,但是由于请求是异步的,因此通常会在检查权限后解决。
When the user open a new window/tab I cannot access the sessionStorage so I need to reload the acl and the user roles from the API, but as the request is asynchronous it normally resolves after the check for permission.
如何证明只有在acl列表加载后stateProvider才会更改页面?
How can I certify that the stateProvider only will change the page after the acl list is loaded?
这是我尝试重新加载权限的方法:
Here is how I'm trying to reload the permission:
myApp.run(['AclService', 'Auth', '$http', function (AclService, Auth, $http) {
if(!AclService.resume()) {
Auth.load_acl();
Auth.load_user_roles();
// I need to certify that these requests are complete before continue
}
}]);
发出请求的服务:
var authServices = angular.module('authServices', []);
authServices.factory('Auth', ['$http', 'AclService', function($http, AclService) {
var authService = {};
authService.load_acl = function() {
return $http.get('/auth/get_acl').then(function(response){
var aclData = {};
for(i in response.data) {
var role = response.data[i];
if(aclData[role.name] === undefined)
aclData[role.name] = [];
aclData[role.name].push(role.ability_name);
}
AclService.setAbilities(aclData);
});
};
authService.load_user_roles = function() {
return $http.get('/auth/get_user_roles').then(function(response){
for(role in response.data) {
AclService.attachRole(response.data[role]);
}
});
};
return authService;
}])
推荐答案
使用状态/路由解析器:
Use state/route resolver:
...
resolve: {
auth: function (AclService, Auth, $q) {
if(!AclService.resume()) {
return $q.all([Auth.load_acl(), Auth.load_user_roles());
}
}
}
如果您需要重用它。
这篇关于如何在AngularJs的stateProvider状态更改之前加载会话数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!