get来获取给定父范围,Angularjs所有子作用域 [英] Get to get all child scopes in Angularjs given the parent scope

查看:149
本文介绍了get来获取给定父范围,Angularjs所有子作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在给定父范围内的所有子作用域的列表。所有我可以从范围的属性发现是$$ childHead,$$ childTail,$$ nextSibling和$$prevSibling。

我现在使用的方法是从父拿到childHead,然后使用nextSibling获得下一个孩子,直到nextSibling为空。

有没有更好的方法吗?
鉴于我要调用一个方法[getModel]上所有的孩子,有没有再次这样做的更好的办法?


解决方案

  

的子指令是使用分离的范围,并在这样的有自己的值不属于从父可见。我想从父作用域访问这些值。


在'角的方式对付父母范围需要访问子作用域问题是将模型向上到父,并有子作用域引用父属性/数据(而不是子作用域拥有自己的局部特性/份)。例如,这就是我们如何处理NG-重复,如果每次迭代中包含,比如,输入表单元素(即每次迭代需要双向数据绑定):<一href=\"http://stackoverflow.com/questions/13714884/difficulty-with-ng-model-ng-repeat-and-inputs\">Difficulty与NG模型,NG-重复,并输入

使用指令,首先要定义在父范围对象的数组,然后让每个孤立子范围的访问,使用了'='符号(即双向数据绑定符号)父范围数组(或单个对象)。因为一个对象被共享的,孤立的范围将引用父对象(他们不会得到一个本地副本)。现在,您对孩子的任何更改作用域属性实际上改变的是父作用域属性。

I would like to know how to get a list of all child scopes given a parent scope. All I can find from the properties of the scope are $$childHead, $$childTail, $$nextSibling and $$prevSibling.

The approach I'm using now is to get the childHead from the parent and then using the nextSibling to get the next child until nextSibling is null.

Is there a better approach? Given that I want to call a method [getModel] on all the children, is there again a better way of doing this?

解决方案

The child directives are using isolated scopes and at such have their own values which are not visible from the parent. I want to access these values from the parent scope.

The 'Angular way' to deal with the issue of "parent scopes needing access to child scopes" is to move the model up into the parent, and have the child scopes reference the parent properties/data (rather than the child scopes having their own local properties/copies). E.g., this is how we deal with ng-repeat if each iteration contains, say, an input form element (i.e., each iteration needs two-way databinding): Difficulty with ng-model, ng-repeat, and inputs

With directives, first define an array of objects in the parent scope, then have each isolated child scope access that parent scope array (or individual object) using the '=' notation (i.e., two-way databinding notation). Because an object is being shared, the isolated scopes will reference the parent objects (they will not get a local copy). Now, any changes you make to the child scopes properties are actually changing the parent scope properties.

这篇关于get来获取给定父范围,Angularjs所有子作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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