获得父界的AngularJS中的所有子女范围
-
11-12-2019 - |
题
我想知道如何获得父范围的所有子范围的列表。所有我都可以从范围的属性找到,是$$儿童母线,$$儿童尾部,$$ ledingsibling和$$ previbling。
我现在正在使用的方法是从父级获取童头,然后使用underibling来获取下一个孩子,直到下面是null。
有更好的方法吗? 鉴于我想在所有的孩子上调用方法[getModel],是否有更好的方法来做这件事?
解决方案
子指令正在使用隔离的范围,并且在这样的有自己的值,这些值是从父级看不到的。我想从父范围内访问这些值。
处理“需要访问子范围的父范围”问题的“角度”是将模型移动到父级,并让子范围引用父属性/数据(而不是子范围拥有自己的本地属性/副本)。例如,如果每次迭代包含,例如,我们如何处理NG重复,例如,如果每个迭代都包含一个输入表单元素(即,每个迭代需要双向数据inding): ng-model难度,ng-freg,和输入
具有指令,首先在父范围内定义一系列对象,然后使用“=”符号(即双向数据绑定符号)具有父范围阵列(或单个对象)的每个孤立的子项范围访问。由于正在共享对象,因此隔离的示波器将引用父对象(它们不会获得本地副本)。现在,您对子范围属性的任何更改实际上都更改了父范围属性。
其他提示
所有角度范围都附加到DOM元素,您可以通过将孩子使用当前元素进行检查,从而以您想要到达的任何孩子。一旦使用以下功能以获得范围。
angular.element('#5th_element').scope();
. 在AngularJS 1.3.2中,将countChildScopes
方法添加到NGMock模块中:
/** * @ngdoc method * @name $rootScope.Scope#$countChildScopes * @module ngMock * @description * Counts all the direct and indirect child scopes of the current scope. * * The current scope is excluded from the count. The count includes all isolate child scopes. * * @returns {number} Total number of child scopes. */ function countChildScopes(scope) { // jshint validthis: true var count = 0; // exclude the current scope var root = scope || angular.element(document).injector().get('$rootScope'); var pendingChildHeads = [root.$$childHead]; var currentScope; while (pendingChildHeads.length) { currentScope = pendingChildHeads.shift(); while (currentScope) { count += 1; pendingChildHeads.push(currentScope.$$childHead); currentScope = currentScope.$$nextSibling; } } return count; }.
使用对象作为返回值以获取ID:
function enumerateChildScopes(scope) { // jshint validthis: true var enum = {}; // exclude the current scope var root = scope || angular.element(document).injector().get('$rootScope'); var pendingChildHeads = [root.$$childHead]; var currentScope; while (pendingChildHeads.length) { currentScope = pendingChildHeads.shift(); while (currentScope) { enum["scope"+pendingChildHeads.length] = currentScope.$id; pendingChildHeads.push(currentScope.$$childHead); currentScope = currentScope.$$nextSibling; } } return enum; }.
引用
不隶属于 StackOverflow