最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call some function after $state.go will finish - Stack Overflow

programmeradmin0浏览0评论

Is it possible to call some function after transition? I mean after loading section. I tried with promise, but it called right after transition started.

$state.go('app.one.two').then(function(d){
    // do staff
});

Thank you!

Is it possible to call some function after transition? I mean after loading section. I tried with promise, but it called right after transition started.

$state.go('app.one.two').then(function(d){
    // do staff
});

Thank you!

Share Improve this question asked Jun 17, 2015 at 7:37 AlexAlex 511 silver badge5 bronze badges 1
  • you should thinnk of resolve option in state – Pankaj Parkar Commented Jun 17, 2015 at 7:41
Add a ment  | 

4 Answers 4

Reset to default 5

You need to look for stateChangeSuccess event. This event will be triggered on every stateChangeSuccess, hence, you have to place a check for your state in order to do some state specific work.

$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { 
    if(toState.name == 'app.one.two') {
       //Do something
    }
})

You could listen to the $state events

  // Check if a state change happened
  $scope.$on('$stateChangeSuccess',
    function onStateSuccess(event, toState, toParams, fromState) {
      //stuff
    }
  );

In the ui-router you can define onEnter and onExit callbacks maybe that might help you.

$state.go return the promise so you have to pass some element after redirection state and then you can add callback function

$state.go('app.one.two', {null:null}).then(function(d){
    // add functionality
});

Here I am passing null value with redirection state, if you have to pass any id or some data to next page then you can pass in {} as a object

发布评论

评论列表(0)

  1. 暂无评论