AngularJs : 取消没有承诺的路由解析
AngularJs : Cancel route resolve without promise
如果登录错误,我需要取消我的路线,这是我的 ANGULARJS NG-ROUTE 代码:
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()
,因为它会重新加载整个页面,而我只需要留在路线上并简单地取消路线,因为只要用户登录错误。
我不知道我可以使用什么代码,我试过这个而不是 $location.path()
: event.preventDefault();
但它不起作用。
我也试过resolve.cancel();
但是还是不行,snif!
如果我删除 $location.path()
,那么解析仍然有效,我可以在用户未登录时看到 "compte" 视图。
换句话说,我不想被重定向,但我想下定决心在密码错误的情况下什么都不做。
也许你有想法?
谢谢。
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.
要取消解析器函数的路由更改,请使用 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'});
})
当解析器函数抛出错误时,将阻止路由更改并广播 $routeChangeError
event。
如果登录错误,我需要取消我的路线,这是我的 ANGULARJS NG-ROUTE 代码:
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()
,因为它会重新加载整个页面,而我只需要留在路线上并简单地取消路线,因为只要用户登录错误。
我不知道我可以使用什么代码,我试过这个而不是 $location.path()
: event.preventDefault();
但它不起作用。
我也试过resolve.cancel();
但是还是不行,snif!
如果我删除 $location.path()
,那么解析仍然有效,我可以在用户未登录时看到 "compte" 视图。
换句话说,我不想被重定向,但我想下定决心在密码错误的情况下什么都不做。
也许你有想法?
谢谢。
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.
要取消解析器函数的路由更改,请使用 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'});
})
当解析器函数抛出错误时,将阻止路由更改并广播 $routeChangeError
event。