如何prevent控制器采用NG-开关时,重装 [英] How to prevent controller reloading when using ng-switch

查看:111
本文介绍了如何prevent控制器采用NG-开关时,重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个例子中,使用纳克开关,我能够不同视图之间切换。
每个视图分配一个控制器。

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

我已经把一个快速的样品在这里在线: http://jsfiddle.net/FBHjZ/1/

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

它看起来像控制器reinstanciated每次我切换视图:
如果输入的输入字段VAL,去家里切换回设置,该值将丢失。

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.

我如何prevent呢?
基本上,我想是从previous观点保持状态,当我视图之间切换。

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

推荐答案

有没有preventing方式现有的 ngSwitch 从重新实例化控制器并重新创建一个新的范围。由于文档该指令中特别指出的是创建一个新的范围和实际创建/销毁相应的DOM元素。

There is no way of preventing the existing ngSwitch from re-instantiating controllers and re-creating a new scope. As noted in the documentation this directive is creating a new scope and actually creates / destroys corresponding DOM elements.

如果你的目标是preserve状态,只是把它在父范围中的一个,请在此的jsfiddle:
http://jsfiddle.net/FBHjZ/2/

If you goal is to preserve state just put it in one of the parent scopes, check this jsFiddle: http://jsfiddle.net/FBHjZ/2/

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

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

这篇关于如何prevent控制器采用NG-开关时,重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆