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

问题描述

我想知道如何获取给定父作用域的所有子作用域的列表.我可以从范围的属性中找到 $$childHead、$$childTail、$$nextSibling 和 $$prevSibling.

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.

我现在使用的方法是从父级获取 childHead,然后使用 nextSibling 获取下一个子级,直到 nextSibling 为 null.

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.

有更好的方法吗?鉴于我想对所有孩子调用一个方法 [getModel],还有没有更好的方法来做到这一点?

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?

推荐答案

处理父作用域需要访问子作用域"问题的Angular 方式"是将模型向上移动到父作用域中,并让子作用域引用父属性/数据(而不是子作用域)拥有自己的本地财产/副本).例如,如果每次迭代都包含一个输入表单元素(即,每次迭代都需要双向数据绑定),这就是我们处理 ng-repeat 的方式:ng-model、ng-repeat 和输入的困难

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.

这篇关于在给定父作用域的情况下获取 Angularjs 中的所有子作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 01:16