AngularJs:取消没有承诺的路线解析 [英] AngularJs : Cancel route resolve without promise
问题描述
如果登录错误,我需要取消路线,这是我的ANGULARJS NG-ROUTE 代码:
I need to cancel my route in case of wrong login, this is my ANGULARJS NG-ROUTE code :
monApp.config(function ($routeProvider, $locationProvider){
var rsf = { // This function tells if user is logged or not
"check":function(stockeUtilisateur,$location,Notification){
u = stockeUtilisateur.getUtilisateur();
if(u.role=='admin'||u.role=='utilisateur'){
Notification.success("Youre logged");
}
else{
$location.path('/accueil'); //redirect user to home.
Notification.error("bad password");
}
}
};
$routeProvider
.when('/accueil', {
templateUrl: 'vues/accueil.html',
controller: 'neutreCtrl'
})
.when('/compte', {
templateUrl: 'vues/compte.html',
controller: 'compteCtrl',
resolve:rsf
})
.otherwise({redirectTo: '/accueil'});
})
问题是,在登录失败的情况下,我不想使用$location.path()
,因为只要用户登录,它就会重新加载整个页面,而我只需要停留在路线上并简单地取消路线即可.是错误的.
The problem is that I don't want to use $location.path()
in the case of login fail, because it reloads the whole page while I simply need to stay on the route and simply cancel the route, as long as user login is wrong.
我不知道可以使用什么代码,我尝试使用它代替$location.path()
:event.preventDefault();
,但是它不起作用.
I don't know what code I could use, I've tried this instead of $location.path()
: event.preventDefault();
but it doesn't work.
我也尝试过resolve.cancel();
,但是它也不起作用,snif!
I've also tried resolve.cancel();
but it doesn't work too, snif !
如果我删除了$location.path()
,则该解决方法仍然有效,并且在用户未登录时我可以看到"compte"视图.
If I remove $location.path()
, then the resolve still works and i can see the "compte" view while the user is not logged.
换句话说,我不想被重定向,但我想解决在密码错误的情况下什么也不做的事情.
IN other words, i don't want to get redirected, but i'd like resolve to simply do nothing in case of bad password.
也许您有个主意吗?
谢谢.
推荐答案
问题是,在登录失败的情况下,我不想使用$ location.path(),因为它会重新加载整个页面,而我只需要停留在路线上,而只需取消路线即可,用户登录错误.
The problem is that I don't want to use $location.path() in the case of login fail, because it reloads the whole page while I simply need to stay on the route and simply cancel the route, as long as user login is wrong.
To cancel a route change from a resolver function, use a throw statement:
monApp.config(function ($routeProvider, $locationProvider){
var rsf = { // This function tells if user is logged or not
"check":function(stockeUtilisateur,$location,Notification){
u = stockeUtilisateur.getUtilisateur();
if(u.role=='admin'||u.role=='utilisateur'){
Notification.success("Youre logged");
}
else{
//$location.path('/accueil');
//Notification.error("bad password");
throw "bad bassword";
}
}
};
$routeProvider
.when('/accueil', {
templateUrl: 'vues/accueil.html',
controller: 'neutreCtrl'
})
.when('/compte', {
templateUrl: 'vues/compte.html',
controller: 'compteCtrl',
resolve:rsf
})
.otherwise({redirectTo: '/accueil'});
})
When a resolver function throws an error, the route change is prevented and a $routeChangeError
event is broadcast.
这篇关于AngularJs:取消没有承诺的路线解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!