How to prevent controller reboot when using ng switch

In this example, using ng-switch, I can switch between different views. Each view is assigned a controller.

I set a quick example here: http://jsfiddle.net/FBHjZ/1/

It seems that the controller is updated every time you switch views: If you enter the value of val in the input field, go to the main page and return to the settings, the value will be lost.

How can I prevent this? Basically, I want to keep state from previous views when switching between views.

+4
source share
1 answer

It is not possible to prevent an existing ngSwitch from re-creating controller instances and re-creating a new scope. As stated in the documentation , this directive creates a new scope and actually creates / destroys the corresponding DOM elements.

If you want to save state, just put it in one of the parent areas, check this jsFiddle: http://jsfiddle.net/FBHjZ/2/

An alternative approach is to use the service for a common model.

+6
source

Source: https://habr.com/ru/post/1440561/


All Articles