是否可以在路由器的解析屬性中使用promise? 我想使用服務來檢查用戶是否有權訪問路徑解析中的頁面。 例子:解決路由解析屬性中的承諾
$stateProvider
.state('user', {
url: '/user',
templateUrl: 'app/views/user/views/user.html',
controller: 'UserCtrl',
controllerAs: 'vm',
resolve: {
security: ['$state', 'userService','messageService','$location', function ($state, userService, messageService, $location) {
userService.checkUserAndRole("user").then(function(data){
var data = userService.checkUserAndRole("user");
if (data.access === false) {
messageService.errorMsg(data.message);
var route = data.route ? data.route:$location.path();
$state.go(route);
});
}]
}
});
我使用$ Q服務userService返回一個承諾
function checkUserAndRole(page){
var defer = $q.defer();
var response = {};
if(!ROLE[page]){
response.access = false;
response.message = "Page is not defined in ROLE config. Please tell admin to correct this!";
response.route = "login";
defer.resolve(response);
}
if(!user.roles){
response.access = false;
response.message = "Logged in user does not seem to have any role data defined. That is why you are redirected";
response.route = "/";
defer.resolve(response);
}
if(!helperService.arrayIntersect(ROLE[page],user.roles)){
response.access = false;
response.message = "You don't have a permission to access '/"+page+"' url";
defer.resolve(response);
}
response.access = true;
response.message = "";
response.route = "";
return defer.promise;
}
如果我不從服務使用承諾,但返回的數據立即一切運作良好,但是,刷新我仍然得到無效的結果,因爲來自服務的用戶數據通過$ http獲取,並且在解決路由時數據還沒有到達。
您好vodich,問題是在決心,你需要它來返回一個承諾,在此刻你的安全屬性什麼都沒有返回。因此,要麼將$ q添加到安全函數中,並在完成所有邏輯後返回defer.promise,要麼更改checkUserAndRole函數以處理狀態更改。 – George
好吧,但我不想將任何數據傳遞給控制器,只需要在決心做這個東西,那就是它。 – vodich