使用转换事件
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
});