1
.state('state1'
{
    name :
    url:
    data:{
      data1=[];
      data2=[];
     }

});

.state('state2'
{
    name :
    url:
    data:{
      data1=[];
      data2=[];
    }

});

Let us say I have n number of states.When user goes from one state to another he is storing data into $state.current.data.

My question is , "is there any shortcut or any technique to reset the data for all the states again", let us say for the scenario when user logs out from the application , I want to reset the data .

I don't want to use $window.location.reload()

Any suggestions/help is appreciated!!

Thanks

WitVault
  • 23,445
  • 19
  • 103
  • 133
shreyansh
  • 1,637
  • 4
  • 26
  • 46

1 Answers1

2

You can use $state.get() to get all the configured states and then reinitialize the data object to a desired value.

Important: child states inherit the data property from their parent states - source. In order not to break the inheritance we'll use angular.copy.

This example reinitializes the data object with {}

angular.forEach($state.get(), function (state) {
  angular.copy({}, state.data);
});

Note that there isn't any way to know the original value of the data object without explicitly specifying or storing it.

Cosmin Ababei
  • 7,003
  • 2
  • 20
  • 34
  • Thank you. But I canot use angular.copy() simply because, next time when user get logs in into the application , will get error like cannot read data1, data2 of undefined, it is because I am not refreshing/reloading the page.So instead of this I have to individually check if(state.data.data1 && state.data.data1.length>0){stata.data.data1=[]} – shreyansh Mar 16 '16 at 04:51