使用轉換事件

UI-Router 公開轉換事件,這些事件有助於處理轉換錯誤,基於某些引數值處理/阻止轉換,自定義身份驗證等。

這些事件可以繫結到 $rootScope 以獲得全域性效果,或者繫結到 $scope 以獲得每個控制器效果。

$stateChangeError - 當嘗試更改狀態失敗並丟擲錯誤時,此事件被廣播,此事件使用以下簽名觸發回撥函式:

callback(event,toState,toParams,fromState,fromParams,error)

event :事件物件

toState :目標狀態

toParams :傳遞給目標狀態的引數

fromState :當前狀態

fromParams :傳遞給當前狀態的引數

錯誤 :錯誤物件

$stateChangeStart - 當狀態轉換開始時廣播此事件,此事件使用以下簽名觸發回撥函式:

callback(event,toState,toParams,fromState,fromParams,options)

options :狀態選項物件

$stateChangeSuccess - 當狀態轉換完成時,廣播此事件,此事件使用以下簽名觸發回撥函式:

callback(event,toState,toParams,fromState,fromParams,options)

$stateNotFound - 當找不到你請求轉換到的狀態時,此事件將被廣播,此事件將使用以下簽名觸發回撥函式:

callback(event,unfoundState,fromParams,fromState)

unfoundState - 表示未找到狀態的物件

例:

$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams, options) {
    $log.debug("$stateChangeSuccess: event: %o toState: %o, toParams: %o, fromState: %o, fromParams: %o, options: %o", event, toState, toParams, fromState, fromParams, options);
    // runs when the state has successfully changed
});

$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams, options) {
    $log.debug("$stateChangeStart: event: %o toState: %o, toParams: %o, fromState: %o, fromParams: %o, options: %o", event, toState, toParams, fromState, fromParams, options);
    // runs when the state has just started to transition
});

$rootScope.$on('$stateNotFound', function (event, unfoundState, fromParams, fromState) {
    $log.debug("$stateNotFound: event: %o unfoundState: %o, fromParams: %o, fromState: %o", event, unfoundState, fromParams, fromState);
    // runs when the state wsa not found
});

$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
    $log.debug("$stateChangeError: event: %o toState: %o, toParams: %o, fromState: %o, fromParams: %o, error: %o", event, toState, toParams, fromState, fromParams, error);
    // runs when there was an error while attempting to transition
});