UI-路由器 state.go 回调状态改变

UI-Router state.go call back on state change

我需要在 state.go 调用成功时回调,并设置我的警报消息。 当前,在调用 state.go 之后,消息被推送到数组。 State.go 调用控制器,并将包含警报消息的数组设置为空。

结果,将不会显示任何警报消息。

控制器:

$scope.alerts = []; // empty array, initialized on startup
.....
// This could be any function
.success(function(data, status, headers, config, statusText){
     $state.go($state.current, {}, {reload : true});
     $scope.alerts.push({type : 'success', msg : status});
})
.error(function(error){
    console.log(error.message);
});

您可以使用状态变化侦听器。

 $rootScope
            .$on('$stateChangeSuccess',
                function (event, toState, toParams, fromState, fromParams) {
                    //show alert()
                });

State Change Events

$state.go()returns一个承诺。

所以做这样的事情:

$state.go('wherever', {whenever: 'whatever'}).then(function() {
  // Get in a spaceship and fly to Jupiter, or whatever your callback does.
});