Save $ scope scope with angularjs

2

I have a screen where some functionality is done via ajax on the page, then the user can filter, etc;

When checking the result he wants, he can click on a link that will redirect him to a new page, but if he clicks the back of the browser he will return to the previous page and will lose the filters he made.

I need to store the variable vm ( $scope-this ) so that when I get back I get this variable and seven again in the variable vm , however it is not working because I store it in a hidden.

Is there anything else I can try?

    
asked by anonymous 11.08.2016 / 15:37

2 answers

1

As already mentioned, you can use SessionStorage or LocalStorage . Angular already has a library that makes it easy to use both: ngStorage

All you have to do is save your filter in one of the two and then retrieve it where you want. Example:

var myApp = angular.module('myApp', ['ngStorage']);

function MyCtrl($scope, $localStorage) {
    //Recupera o objeto que está no localStorage
    $scope.myFilter = $localStorage.filter;
    console.log($scope.myFilter);
  
    $scope.filtrate = function(data) {
        //Salva o objeto no localStorage
        $localStorage.filter = data;
        console.log($localStorage.filter);
    };
  
    $scope.clearStorage = function(){
      //Limpa o localStorage de nome filter
      delete $localStorage.filter;
      console.log($localStorage.filter);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/ngStorage/0.3.11/ngStorage.min.js"></script>

<div ng-app="myApp" ng-controller="MyCtrl">
 <input type="text" ng-model="user.name" placeholder="Name"/>
 <input type="text" ng-model="user.age" placeholder="Age"/>
 <button ng-click="filtrate(user)">Filter</button>
 <button ng-click="clearStorage()">Clear Storage</button>
</div>

As WebStorage is not supported in the code snippet follows the example in jsfiddle

    
12.08.2016 / 13:50
-1

There are several methods to do this, particularly I'd rather use services without a bit of code gets a bit tricky to help more. Depending on the situation you can put all the logic inside the service.

In this case that does a reload of the whole page it really loses everything, the way it would look something like the localstorage itself, but maybe you're more interested in session storage , which is the same thing, but unlike locastorage the data is deleted as soon as the browser closes.     

11.08.2016 / 17:51