UI-Router状态。回到状态改变

时间:2020-12-25 19:40:06

I need a callback when state.go has been invoked successfully, and set my alert message. Currently the message is pushed to the array, after state.go has been called. State.go calls the controller, and the array containing the alert message is set to empty.

状态时需要回调。go已被成功调用,并设置了我的警报消息。当前消息被推送到数组after state。被称为。状态。go调用控制器,包含警报消息的数组被设置为空。

Result, no alert message will be shown.

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

Controller:

控制器:

$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);
});

2 个解决方案

#1


88  

$state.go() returns a promise.

$ state.go()返回一个承诺。

So do something like:

所以做一些像:

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

#2


6  

You may use state change listener.

您可以使用状态更改侦听器。

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

See State Change Events.

看到状态变化的事件。

#1


88  

$state.go() returns a promise.

$ state.go()返回一个承诺。

So do something like:

所以做一些像:

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

#2


6  

You may use state change listener.

您可以使用状态更改侦听器。

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

See State Change Events.

看到状态变化的事件。