Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I took a bit of time to work out what is the best way of doing this. I also wanted to keep the state, when the user leaves the page and then presses the back button, to get back to the old page; and not just put all my data into the <em>rootscope</em>.</p> <p>The final result is to have a <em>service</em> for each <em>controller</em>. In the controller, you just have functions and variables that you dont care about, if they are cleared.</p> <p>The service for the controller is injected by dependency injection. As services are singletons, their data is not destroyed like the data in the controller.</p> <p>In the service, I have a model. the model ONLY has data - no functions -. That way it can be converted back and forth from JSON to persist it. I used the html5 localstorage for persistence.</p> <p>Lastly i used <code>window.onbeforeunload</code> and <code>$rootScope.$broadcast('saveState');</code> to let all the services know that they should save their state, and <code>$rootScope.$broadcast('restoreState')</code> to let them know to restore their state ( used for when the user leaves the page and presses the back button to return to the page respectively).</p> <p>Example service called <strong>userService</strong> for my <strong>userController</strong> :</p> <pre><code>app.factory('userService', ['$rootScope', function ($rootScope) { var service = { model: { name: '', email: '' }, SaveState: function () { sessionStorage.userService = angular.toJson(service.model); }, RestoreState: function () { service.model = angular.fromJson(sessionStorage.userService); } } $rootScope.$on("savestate", service.SaveState); $rootScope.$on("restorestate", service.RestoreState); return service; }]); </code></pre> <p><strong>userController</strong> example</p> <pre><code>function userCtrl($scope, userService) { $scope.user = userService; } </code></pre> <p>The view then uses binding like this</p> <pre><code>&lt;h1&gt;{{user.model.name}}&lt;/h1&gt; </code></pre> <p>And in the <em>app module</em>, within the run function i handle the <em>broadcasting</em> of the <em>saveState</em> and <em>restoreState</em></p> <pre><code>$rootScope.$on("$routeChangeStart", function (event, next, current) { if (sessionStorage.restorestate == "true") { $rootScope.$broadcast('restorestate'); //let everything know we need to restore state sessionStorage.restorestate = false; } }); //let everthing know that we need to save state now. window.onbeforeunload = function (event) { $rootScope.$broadcast('savestate'); }; </code></pre> <p>As i mentioned this took a while to come to this point. It is a very clean way of doing it, but it is a fair bit of engineering to do something that i would suspect is a very common issue when developing in Angular.</p> <p>I would love to see easier, but as clean ways to handle keeping state across controllers, including when the user leaves and returns to the page.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload