ngRouter/uiRouter preserve controllers when changing view
-
26-12-2019 - |
Question
Is there anyway to preserve route controllers in Angular when using routing? Everytime I change the view, the old controller is destroyed and a new one created, but I was wondering if there was a way to preserve an existing controller.
So right now, if the user started to complete a form, then navigates away and comes back to the original view, the controller has been re-created and the users form data lost. Where as if the controller was preserved the user could pick up again.
I know the real solution here is to either have a parent controller with the current model or a service to retain state, however I was wondering if anyone else had a similar experience where they wanted to preserve a view controller?
Solution
If you want behavior like that then I think you have to somehow store the information of the uncompleted form in a service. I don't think you can change the behavior of how angular destroys and creates new instances of controllers when navigating through your application. This seems to me as a fundamental solution in Angular.