AngularJs : Cancel route resolve without promise

谁说我不能喝 提交于 2019-12-12 18:03:33

问题


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'}); 
})

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.

I don't know what code I could use, I've tried this instead of $location.path() : event.preventDefault(); but it doesn't work.

I've also tried resolve.cancel(); but it doesn't work too, snif !

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.

Maybe you have an idea ?

Thank you .


回答1:


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.



来源:https://stackoverflow.com/questions/45169638/angularjs-cancel-route-resolve-without-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!