Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs UI路由器状态。状态更改时回拨_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs UI路由器状态。状态更改时回拨

Angularjs UI路由器状态。状态更改时回拨,angularjs,angular-ui-router,Angularjs,Angular Ui Router,成功调用state.go后,我需要回调,并设置我的警报消息。 当前,在调用state.go之后,消息被推送到数组中。 State.go调用控制器,并且包含警报消息的数组被设置为空 结果,不会显示任何警报消息 控制器: $scope.alerts = []; // empty array, initialized on startup ..... // This could be any function .success(function(data, status, headers, confi

成功调用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.go()
返回承诺

所以你可以这样做:

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

$state.go($state.current,{},{reload:true})的用途是什么?使用rootscope很糟糕。我一点也不喜欢这个答案。一定有更好的方法。@Fuser97381为什么使用rootscope很糟糕?@RocoCTZ污染全局变量空间通常被认为是个坏主意。在这种情况下,这是不好的,因为您将代码放在只需要在一个特定上下文上运行的全局上下文中。它污染了全球环境,这是不好的。请在这里更广泛地阅读这个问题:@Fuser97381尽管您所说的总体上是正确的,但这并不意味着全局变量总是不好的。我肯定认为没有rootscope应该是一种更好的方法,但是使用它对我来说并不可怕:你试图跨上下文做一些事情。我不认为这是一个坏主意,因为路由是以全局方式进行的,而rootscope是全局的,而且$stateChangeSuccess对于任何特定的状态都不是特别的,是一般性的。@KirillGusyatin这没什么,宝贝。非常感谢$scope.stateGo=function(href){$state.go(href)。然后(function(){//do something};}//html