2016-08-21 61 views
3

我的問題是,UI路由器$stateChangeError不是由「404」 invaild狀態觸發的狀態。

我確實知道我對$stateChangeSucess火災正確:

$rootScope.$on('$stateChangeSuccess', 
     function(event, toState, toParams, fromState, fromParams) 

當我故意使無效狀態,像這樣:

<ui-view> 
    <a ui-sref="foo" style="cursor: pointer;">Foo is not a state</a> 
</ui-view> 

則不觸發$stateChangeError事件,我知道我有正確的簽名

angular 
    .module('blocks.router') 
    .run(['$rootScope', function($rootScope) { 
     $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { 
      console.log('$stateChangeError fired!'); 
     }); 
    }]); 

而且我加m y功能在同一時間我加我的工作$stateChangeSuccess

什麼,我可能是做錯了什麼建議?

+0

你執行'$ urlRouterProvider.otherwise()'?如果是這樣,這就是爲什麼無效狀態不會觸發錯誤 – Phil

+0

不,我目前沒有實現$ urlRouterProvider.otherwise() –

+0

@Phil添加$ urlRouterProvider.otherwise(「/」)沒有觸發錯誤 –

回答

2

所以「$ stateChangeError」不會觸發無效/拼錯的狀態。例如,如果您聲明的是「登錄」,並且您在控制器中將狀態更改爲「loginfoo」,則「$ stateChangeError」不會觸發。

現在,如果您的templateUrl狀態「login」的位置是「app/templates/login.html」,並且您錯誤地命名了模板「app/templates/loginfoo.html」,則$ stateChangeError會在您轉到「登錄」狀態。

所以「$ stateChangeError」火了一個有效的狀態請求無法找到控制器,模板,資源爲有效狀態。但它不會因爲拼寫錯誤的狀態而不起作用。

+2

失敗的'resolve'承諾也會觸發狀態更改錯誤 – Phil

相關問題