AngularJS$routeChangeStart仅限首次事件

AngularJS$routeChangeStart仅限首次事件,angularjs,Angularjs,有没有办法只听第一条路线的开始而不听其他路线的开始?Obvs这可以用一个变量来完成,但不知道是否有一个事件来代替它 $rootScope.$on("$routeChangeStart", function (event, next, current) { Auth.authorize().then(function(){ // only want to happen the first time

有没有办法只听第一条路线的开始而不听其他路线的开始?Obvs这可以用一个变量来完成,但不知道是否有一个事件来代替它

       $rootScope.$on("$routeChangeStart", function (event, next, current) {
            Auth.authorize().then(function(){
                // only want to happen the first time
                // $location.path('dlp');
            },function(){
                $location.path('login');
            });
        });

如果将$rootScope.$分配给变量并将其作为函数调用,则可以在广播中取消订阅它:

var offCallMeFn = $rootScope.$on("$routeChangeStart", function(event, next, current){
        Auth.authorize().then(function(){
            // only want to happen the first time
            // $location.path('dlp');
        },function(){
            $location.path('login');
        });
});

 //this will deregister that listener
offCallMeFn();
但是,对于处理授权而言,这似乎是一个坏主意,因为用户可能手动访问路由(例如,从浏览器历史记录)或可能注销


来源:

如果将$rootScope.$分配给变量并将其作为函数调用,则可以在广播中取消订阅该$rootScope.$:

var offCallMeFn = $rootScope.$on("$routeChangeStart", function(event, next, current){
        Auth.authorize().then(function(){
            // only want to happen the first time
            // $location.path('dlp');
        },function(){
            $location.path('login');
        });
});

 //this will deregister that listener
offCallMeFn();
但是,对于处理授权而言,这似乎是一个坏主意,因为用户可能手动访问路由(例如,从浏览器历史记录)或可能注销


来源:

如果将$rootScope.$分配给变量并将其作为函数调用,则可以在广播中取消订阅该$rootScope.$:

var offCallMeFn = $rootScope.$on("$routeChangeStart", function(event, next, current){
        Auth.authorize().then(function(){
            // only want to happen the first time
            // $location.path('dlp');
        },function(){
            $location.path('login');
        });
});

 //this will deregister that listener
offCallMeFn();
但是,对于处理授权而言,这似乎是一个坏主意,因为用户可能手动访问路由(例如,从浏览器历史记录)或可能注销


来源:

如果将$rootScope.$分配给变量并将其作为函数调用,则可以在广播中取消订阅该$rootScope.$:

var offCallMeFn = $rootScope.$on("$routeChangeStart", function(event, next, current){
        Auth.authorize().then(function(){
            // only want to happen the first time
            // $location.path('dlp');
        },function(){
            $location.path('login');
        });
});

 //this will deregister that listener
offCallMeFn();
但是,对于处理授权而言,这似乎是一个坏主意,因为用户可能手动访问路由(例如,从浏览器历史记录)或可能注销


来源:

你为什么要这样做?你为什么要这样做?你为什么要这样做?你为什么要这样做?