1


I my angular app, I have a list of items shown in one page and on clicking continue button, the app will move to another state. In that page there is a "Back" button which has the code $state.go('prevPage') on ng-click event. But on coming back all the data are cleared. Is there any way I can keep the scope variables alive on returning back ?

Update
What I tried..

On going to another state, saved all the scope data in a service

returnItemModel.saveReturnData($scope);

While coming back retrived it and assigned it to scope and I call the digest cycle.

var savedReturnData = returnItemModel.restoreReturnData();
if(savedReturnData){ 
    $scope = savedReturnData;
    $scope.$apply();
}

This is not working. Does someone know the reason.

I'm nidhin
  • 2,592
  • 6
  • 36
  • 62

2 Answers2

1

The scope values get destroyed upon routing to another state. You can use angular services like service, factory, or values to persist value when the scope is destroyed. This can done using setting a listener for destroy event in controller.

$scope.$on("destroy", function(){
  // data to keep alive
});

Update

you cannot do like this: $scope = savedReturnData;. You have to do somthing like this. $scope.prevScope = savedReturnData. And then you can use the previous scope in you view like {{ prevScope.someVar }}

Muhammed Neswine
  • 2,028
  • 1
  • 20
  • 20
  • Its not working. Actually, its not just one or two variables. there are a number of vata associated to that scope. I want all of them like how they were before. That is why I took this approach. – I'm nidhin Apr 04 '17 at 14:30
1

You need to put those values in either service or factory as scope values gets destroyed upon moving to another state.

kaushlendras
  • 220
  • 1
  • 7