AngularJS | handle routing before they load

后端 未结 5 977
我在风中等你
我在风中等你 2020-12-07 17:10

I wish to create a simple authentication check for my routes by external service.

I define the access requirements on the route object:

$routeProvide         


        
5条回答
  •  有刺的猬
    2020-12-07 17:49

    Use $routeProvider resolve

    .when('/', {
        templateUrl: 'src/app/views/index.html',
        controller: 'indexCtrl',
        resolve: function($q, $location) {
          var deferred = $q.defer(); 
          deferred.resolve();
          if (!isAuthenticated) {
             $location.path('/login');
          }
    
          return deferred.promise;
        }
    })
    

提交回复
热议问题