0

Within AngularJS, is there a way of preserving the state of a model & view between invocations?

Say, you have a master view that displays a tables or tables of information; the user has manipulated the view and applied filters and sort orders, etc to the information on display in this master view. The user then selects an item on this master view and is taken to another, child view, that displays information about the item they have selected; if the user then decides to return to the previous view I want that view to be in the same state as it was before, with its filters etc intact.

I'm currently using the technique described here: Maintain model of scope when changing between views in AngularJS where I preserve the model state using a service and that service is then injected back into the controller when its recreated. Is this the AngularJS way, or is there a better way of preserving model/view state between invocations?

Community
  • 1
  • 1
Barry Carr
  • 432
  • 4
  • 11
  • 1
    You could update `$location.path()` and/or `$location.search()` upon changes and have your main view respect those values. – Yoshi Jul 03 '14 at 09:26
  • @Yoshi I didn't know about $location, I'll take a look at that. Thanks for the suggestion. – Barry Carr Jul 03 '14 at 09:37
  • IMHO service is best for it, using $location sounds like stubborn workaround to not make things the angularjs way – maurycy Jul 03 '14 at 09:39
  • @maurycy I would disagree, using `$location` has the benefit of the view being indepent of the application state. One could copy/store the url for later use or for maintaining deep links where needed. Only using a service without any kind of persistence can make a SPA very *fragile* to use. Also if one doesn't want to directly use `$location` in the controller it's easily hidden behind some custome service. – Yoshi Jul 03 '14 at 09:45
  • 1
    I generally create a store service that I inject on-demand. I use HTML5 storage within it. – Pak Jul 03 '14 at 09:45
  • Addition: I strongly believe that not utilizing the potential of the URL is basically the same as using framesets back from the dark ages. And thanks a lot that those times are over. – Yoshi Jul 03 '14 at 11:00

0 Answers0