AngularJS $ routeProvider解决方法不起作用 [英] AngularJS $routeProvider Resolve Method Not Working
本文介绍了AngularJS $ routeProvider解决方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我定义了以下两条路线:
In my code, I define the following two routes:
$routeProvider.when('/problem/report', {
templateUrl: '/app/management/problem/reportAProblem.html',
controller: 'reportAProblemCtrl',
resolve: {
authorized: function($http, $location) {
var path = $location.path();
return $http.get('/svc/authorize/view?urlPath=' + path).then(function(response) {
var data = response.data;
if (response.data.result === 'NOT_AUTHORIZED') {
throw "NOT_AUTHORIZED";
}
return data;
})
}
}
});
$routeProvider.when('/problem', {
templateUrl: '/app/management/problem/problem.tmpl.html',
controller: 'problemCtrl',
resolve: {
authorized: ['$authorization', function($authorization) {
$authorization.authorize();
}]
}
});
第一种情况似乎有效.但是,第二种情况已将函数重构为Service,并且AngularJS在显示页面之前似乎并没有等待Promise解决.
The first case seems to work. However, the second case has had the function refactored into a Service, and AngularJS does not seem to be waiting for the Promise to resolve before displaying the page.
重构后的代码如下:
angular.module('authorization', [])
.factory('$authorization', ['$http', '$location',function($http, $location) {
var $authorization = {};
$authorization.authorize = function() {
var path = $location.path();
return $http.get('/svc/authorize/view?urlPath=' + path).then(function(response) {
var data = response.data;
if (response.data.result === 'NOT_AUTHORIZED') {
throw "NOT_AUTHORIZED";
}
return data;
});
}
return $authorization;
}]);
有人可以告诉我为什么上面的第二种情况在显示页面之前不等待诺言解决吗?
Can anyone tell me why the second case above doesn't wait for the promise to resolve before displaying the page?
推荐答案
添加收益:
authorized: ['$authorization', function($authorization) { **return** $authorization.authorize(); }]
这篇关于AngularJS $ routeProvider解决方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文