angularjs:从控制器NG-交换机内更改父范围 [英] angularjs: Change parent scope from controller within a ng-switch

查看:116
本文介绍了angularjs:从控制器NG-交换机内更改父范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我可以改变从子控制器模型值,但是当孩子控制器处于NG-开关则不起作用,为什么呢?我创建为例来证明它。

So, I can change a model value from child controller, but when the child controller is in ng-switch then it doesn't work, why? I created an example to demonstrate it.

要避免这种情况的一种方法是使用模型的名称,如 bunnies.kills 。这是一个bug或者这是一个功能?

One way to avoid this is to use the . in model name, like bunnies.kills. Is this a bug or this is a feature ?

采用了棱角分明1.0.6

Using Angular 1.0.6

推荐答案

在您的孩子的控制器使用您code结构,你需要改变:

Using your code structure, in your child controllers you would need to change:

$scope.$parent.kills++;

$scope.$parent.$parent.kills++;

说明:的 MainCtrl 范围 SimpleParentCtrl 的父范围,但的祖父母 Step1Ctrl Step2Ctrl 。正如一些人指出的那样, NG-开关创建其自己的范围,那么你的 Step1Ctrl Step2Ctrl 每个创建的 NG-开关。

Explanation: MainCtrl's scope is the parent scope of SimpleParentCtrl, but the grandparent of Step1Ctrl and Step2Ctrl. As some others pointed out, ng-switch creates its own scope, and then your Step1Ctrl and Step2Ctrl each created a child scope of the ng-switch.

注意:每次1或2个按钮后,无论是 NG-开关和它的当前匹配的子控制器获得一个新的作用域

Note: Each time the 1 or 2 button is clicked, both the ng-switch and it's currently matched child controller get a new scope.

另外:如果你恰巧是看在角源,不知道的 NG-开关指令怎么没有创建自己的范围范围属性,则答案是人工手动确实在它的连接方法,通过范围。$新的()。该指令 NG-包括 NG-开关 NG-重复 NG-视图所有新建作用域这样一来,无论是在链接方法或编译方法的返回链接功能。

Also: In case you happen to be looking in the Angular source and wondering how the ng-switch directive creates its own scope without a scope property, the answer is that it does so manually in its link method via scope.$new(). The directives ng-include, ng-switch, ng-repeat, and ng-view all create new scope this way, either in the link method or the compile method's returned link function.

资源:

https://github.com/angular/angular.js/wiki/The-Nuances-of-Scope-Prototypal-Inheritance
http://www.youtube.com/watch?v = ZhfUv0spHCY和放大器;配备= youtu.be& T公司=30米

这篇关于angularjs:从控制器NG-交换机内更改父范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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