0

I'm using angular 1.5.11 and angular-ui-router 1.0.0-beta.3.

I've implemented "websudo" which is similar to what Github does---it takes you to a login page if you last logged in a long time ago. Upon logging back in, it takes you back to the page that you were in. The catch is that the component you were on should not reload (this way you can resume work). I've tried the following but the component always reloads after logging back in and being redirected to the component's route:

$state.go('component.route', {}, {reload: false, notify: false});

Is there possibly an alternative way to implement this functionality?

U Avalos
  • 6,538
  • 7
  • 48
  • 81

1 Answers1

0

If you store all your data in factories/services that are referenced from the view (inject into controller then put on scope and reference properties in the factory/service from the view) then the data is persisted so long as the page is running.

JS

angular.module('myMod',[])
  .service('PersistData', function(){
    return {}
  })
  .controller('MyCtrl', function(PersistData){ this.PersistData = PersistData})

HTML

<div ng-app="myMod" ng-controller="MyCtrl as myCtrl">
  <input type="text" ng-model="myCtrl.PersistData.first_name"/>
</div>

The angular $injector will create the PersistData object the first time it's referenced then keep a reference to it for any other calls to get/inject it (singleton).

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • No, creating a global instance isn't moving the code base in the right direction – U Avalos Nov 29 '17 at 17:35
  • @UAvalos so you plan to store the data when you leave the state where the controller instance is destroyed and you come back and it's created but you plan on using an object in that scope? I have news for you that isn't going to work. You need to either persist the data in some sort "global" object (services are used everywhere in angular if you're smart your controllers are slim and services widespread) or you store it in local storage or something "more global" than a service. Just because you read somewhere using singeltons is an antipattern means it is 100% true all the time. – shaunhusain Nov 29 '17 at 17:45
  • Let me know when you find your solution that is immutable and pure functional and TDD (and doesn't exist) or decide to do something more hacky than the proposed solution like stringifying your model and stuffing it in the URL or local storage. – shaunhusain Nov 29 '17 at 19:27
  • READ dude... @shaunhusain I want to prevent the controller instance from being destroyed!!!! – U Avalos Nov 30 '17 at 19:21
  • @UAvalos when you "leave a state" you are getting rid of the controller and scope and anything related to it https://stackoverflow.com/questions/16094940/what-is-the-lifecycle-of-an-angularjs-controller – shaunhusain Nov 30 '17 at 19:44
  • Also maybe consider not being rude when asking for help... so far your solution seems to be a non starter – shaunhusain Nov 30 '17 at 20:32
  • well if you're saying that there's no way to keep the route states from being re-used, then ahh.... yes, this is unfortunately, an answer. Thinking about it a little bit more, it's no different than redux state – U Avalos Dec 01 '17 at 05:08