4

In this example, using ng-switch, I'm able to switch between different views. Each view is assigned a controller.

I've put a quick sample online here : http://jsfiddle.net/FBHjZ/1/

It looks like the controller is reinstanciated everytime I switch views : If you enter a val in the input field, go to home and switch back to settings, the value is lost.

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

4

1 回答 1

6

无法阻止现有ngSwitch控制器重新实例化控制器并重新创建新范围。如文档中所述,该指令正在创建一个新范围并实际创建/销毁相应的 DOM 元素。

如果您的目标是保留状态,只需将其放入父范围之一,请检查此 jsFiddle:http: //jsfiddle.net/FBHjZ/2/

另一种方法是使用共享模型的服务。

于 2012-10-18T17:23:55.443 回答