Skip to content Skip to sidebar Skip to footer

How To Prevent Specific $state From Refreshing When Another $state Updates

https://plnkr.co/edit/bOZW1a9u62W1QA6cYjYj?p=preview Expected After Login all $states initialize, then after clicking a Ticker button, the only $states that should re-init are tick

Solution 1:

I could resolve this issue depending on which state you're coming from. Based on the fact that you only want the feed to be loaded 'once' when dashboard is loaded, I'm checking the name of the previous state, and if it is not dashboard, only then I'll proceed with executing the rest of the controller code. Here is the plunker. Note that this is not very scalable approach if more states are added and if someone else has a better approach, I'd really like to see that.

I changed the controller code as follows:

feed.component('feedModule', {
  templateUrl: 'feed-module-template.html',
  controller: function($scope, $state) {
    var previousState = $state.params.previousState.name;
    if(previousState !== 'dashboard') {
      console.log('Feed init (only once)', $state.params); //do whatever u want here
    }
  }
});

I'm passing around previous state as params to other states like this:

controller: function($state) {
  this.login = function() {
    $state.go('dashboard', {previousState : { name : $state.current.name }})
  }
}

Look at the full plunker here to see if this helps your case!

Post a Comment for "How To Prevent Specific $state From Refreshing When Another $state Updates"