2017-01-21 50 views
1

我使用this來創建我的網站,當我創建一個運行方法$ rootScope.on('$ stateChangeStart')並且不工作。下面是代碼:

.run('Url',function($rootScope, $state, $location){ 
    'ngInject' ; 

    console.log('Is working'); 
    function message(to, toP, from, fromP) { 
     return from.name + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP); 
    } 
    $rootScope.$on("$stateChangeStart", function(evt, to, toP, from, fromP)  { 
     console.log("Start: " + message(to, toP, from, fromP)); 
     console.log('Not working'); 
    }); 

第一次登錄時,我下載該網站的工作,但後兩個日誌都沒有,任何想法問題出在哪裏?

回答

0

你可以使用rootScope美元等作爲

.run(['$rootScope', '$state', '$location',function($rootScope, $state, $location){ 
     console.log('Is working'); 
     function message(to, toP, from, fromP) { 
      return from.name + angular.toJson(fromP) + " -> " + to.name + angular.toJson(toP); 
     } 
     $rootScope.$on("$stateChangeStart", function(evt, to, toP, from, fromP)  { 
      console.log("Start: " + message(to, toP, from, fromP)); 
      console.log('Not working'); 
     }) 
    }]); 
+0

仍然沒有工作,任何其他建議? – Wolffyx

0

這個嘗試: '$ viewContentLoading' 或 '$ viewContentLoaded':

$rootScope.$on('$viewContentLoading', function() { 
    // your stuff here… 
}); 
$rootScope.$on('$viewContentLoaded', function() { 
    // your stuff here… 
}); 
0

如果更改的版本。

對於新的ui路由器(Angular 1.x版本)。您需要加載stateEvents文件。

<script src="lib/angular-ui-router/release/stateEvents.min.js"></script> 

負載添加在你的應用程序

angular.module('MyApp', ['ui.router', 'ui.router.state.events', ...