这是我针对特定路线的ui-router
配置
state('app.merchant', {
url: '/start/merchant',
views: {
'mainView': {
templateUrl: "partials/start_merchant.html"
}
},
css: ['assets/vendor/bootstrap/dist/css/bootstrap.css','assets/css/styles.css','assets/css/plugins.css'],
title: 'Buttons',
resolve: {
userRequired: userRequired,
}
resolve: loadSequence('flow','angularFileUpload','MerchantWizardCtrl')
})
问题是,即使页面不符合userRequired
要求,也会显示该页面。这是userRequired
:
function userRequired($q, $location, $auth,Account) {
var deferred = $q.defer();
if ($auth.isAuthenticated()) {
Account.getUserStatus()
.then(function(response){
if(response.data == true){
deferred.resolve();
}
else{
deferred.reject();
}
})
.catch(function(response){
console.log("Error has occur, Please contact adminstrator");
});
} else {
deferred.resolve();
}
return deferred.promise;
}
如何解决这个问题?谢谢!
修改
loadsequence:
function loadSequence() {
var _args = arguments;
return {
deps: ['$ocLazyLoad', '$q',
function ($ocLL, $q) {
var promise = $q.when(1);
for (var i = 0, len = _args.length; i < len; i++) {
promise = promiseThen(_args[i]);
}
return promise;
function promiseThen(_arg) {
if (typeof _arg == 'function')
return promise.then(_arg);
else
return promise.then(function () {
var nowLoad = requiredData(_arg);
//console.log(nowLoad)
if (!nowLoad)
return $.error('Route resolve: Bad resource name [' + _arg + ']');
return $ocLL.load(nowLoad);
});
}
function requiredData(name) {
if (jsRequires.modules)
for (var m in jsRequires.modules)
if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
return jsRequires.modules[m];
return jsRequires.scripts && jsRequires.scripts[name];
}
}]
};
}
答案 0 :(得分:2)
当getUserStatus()服务抛出异常时(在.catch()中),并且当Authenticated必须拒绝时,您不会拒绝承诺,请尝试此解决方案:
state('app.merchant', {
url: '/start/merchant',
views: {
'mainView': {
templateUrl: "partials/start_merchant.html"
}
},
css: ['assets/vendor/bootstrap/dist/css/bootstrap.css','assets/css/styles.css','assets/css/plugins.css'],
title: 'Buttons',
resolve: {
userRequired: userRequired,
loadSequence: loadSequence('flow','angularFileUpload','MerchantWizardCtrl')
}
})
function userRequired($q, $location, $auth,Account) {
return $q(function(resolve, reject) {
if ($auth.isAuthenticated()) {
Account.getUserStatus()
.then(function(response){
if(response.data == true){
resolve();
}
else{
reject();
}
})
.catch(function(response){
console.log("Error has occur, Please contact adminstrator");
reject()
});
} else {
reject()
}
});
}
function loadSequence() {
var _args = arguments;
return ['$ocLazyLoad', '$q', function ($ocLL, $q) {
var promise = $q.when(1);
for (var i = 0, len = _args.length; i < len; i++) {
promise = promiseThen(_args[i]);
}
return promise;
function promiseThen(_arg) {
if (typeof _arg == 'function')
return promise.then(_arg);
else
return promise.then(function () {
var nowLoad = requiredData(_arg);
//console.log(nowLoad)
if (!nowLoad)
return $.error('Route resolve: Bad resource name [' + _arg + ']');
return $ocLL.load(nowLoad);
});
}
function requiredData(name) {
if (jsRequires.modules)
for (var m in jsRequires.modules)
if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
return jsRequires.modules[m];
return jsRequires.scripts && jsRequires.scripts[name];
}
}]
}
将延迟方法更改为Promise A +方法(我推荐这种方式)。
在.catch中添加reject()
答案 1 :(得分:0)
我认为问题是您正在返回承诺,根据设计,它会允许您的应用在尝试确定<img src="${pageContext.request.contextPath}/photo/place/${place.id}/small">,
是否有效时继续加载
因此,不要像使用promise这样的异步方法,只需同步解析该依赖关系,它就会阻止页面加载。
以下是我如何修改UserRequired
:
UserRequired
答案 2 :(得分:0)
首先,您的配置对象具有两倍相同的属性:
{
resolve: {userRequired: userRequired}
resolve: loadSequence('flow','angularFileUpload','MerchantWizardCtrl')
}
你应该有类似的东西:
{
resolve : {
userRequired : userRequired,
loadSequence : loadSequence('flow','angularFileUpload','MerchantWizardCtrl')
}
此外,您的捕获应该返回被拒绝的承诺
return $q.reject();
如上所述,未经过身份验证时拒绝。 您可以在chrome中进行调试,以确保调用所有promise并返回正确的值。