How do I check for login or other status before launching a route in Angular with routeProvider?

后端 未结 3 2067
耶瑟儿~
耶瑟儿~ 2021-02-14 11:39

Let\'s say I have 4 routes - 2 require the user to be logged in, 2 do not. My app init looks like:

        $routeProvider.when(\'/open1\',{templateUrl:\'/open1.h         


        
3条回答
  •  终归单人心
    2021-02-14 11:45

    I had the same problem and I did it this way:

    var app = angular.module('myModule',["ui-bootstrap"]);
    

    And then listen for a locationchange in the app (this will also trigger onEnter of a page)

    app.run(function ($rootScope, $location, $cookieStore) {
     $rootScope.$on("$locationChangeStart", function (event, next, current) {
        //Here you can check whatever you want (servercall, cookie...)
     });
    }
    

    I Hope this helps!

提交回复
热议问题