获取给定父作用域的Angularjs中的所有子作用域
我想知道如何获得给定父作用域的所有子作用域的列表。我从作用域的属性中只能找到$$childHead、$$childTail、$$nextSibling和$$previsibling 我现在使用的方法是从父级获取childHead,然后使用nextSibling获取下一个子级,直到nextSibling为null 有更好的方法吗?获取给定父作用域的Angularjs中的所有子作用域,angularjs,angularjs-scope,parent-child,enumeration,Angularjs,Angularjs Scope,Parent Child,Enumeration,我想知道如何获得给定父作用域的所有子作用域的列表。我从作用域的属性中只能找到$$childHead、$$childTail、$$nextSibling和$$previsibling 我现在使用的方法是从父级获取childHead,然后使用nextSibling获取下一个子级,直到nextSibling为null 有更好的方法吗? 假设我想对所有子元素调用方法[getModel],那么还有更好的方法吗?所有角度范围都附加到DOM元素,您可以从使用当前元素检查子元素开始,直到您想要访问的任何子元素。
假设我想对所有子元素调用方法[getModel],那么还有更好的方法吗?所有角度范围都附加到DOM元素,您可以从使用当前元素检查子元素开始,直到您想要访问的任何子元素。到达后,使用下面的函数获取范围
angular.element('#5th_element').scope();
子指令使用独立的作用域,并且在这种情况下,它们有自己的值,而这些值在父指令中是不可见的。我想从父范围访问这些值
处理“需要访问子作用域的父作用域”问题的“角度方法”是将模型向上移动到父作用域中,并使子作用域引用父属性/数据(而不是子作用域具有自己的本地属性/副本)。例如,如果每个迭代都包含输入表单元素(即每个迭代都需要双向数据绑定),我们就是这样处理ng repeat的:
使用指令,首先在父作用域中定义一个对象数组,然后让每个独立的子作用域使用“=”符号(即双向数据绑定符号)访问该父作用域数组(或单个对象)。因为正在共享一个对象,所以隔离作用域将引用父对象(它们不会获得本地副本)。现在,您对子作用域属性所做的任何更改实际上都在更改父作用域属性。在AngularJS 1.3.2中,ngMock模块中添加了一个
countChildScopes
方法:
/**
* @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;
}
/**
*@ngdoc方法
*@name$rootScope.Scope#$countChildScopes
*@ngMock模块
*@说明
*统计当前作用域的所有直接和间接子作用域。
*
*当前范围被排除在计数之外。计数包括所有隔离子作用域。
*
*@返回{number}子作用域的总数。
*/
函数countChildScopes(作用域)
{
//这是真的
var count=0;//排除当前范围
var root=scope | | angular.element(document.injector().get(“$rootScope”);
var pendingChildHeads=[root.$$childHead];
无功电流范围;
while(pendingChildHeads.长度)
{
currentScope=pendingChildHeads.shift();
while(当前范围)
{
计数+=1;
pendingChildHeads.push(当前范围$$childHead);
currentScope=currentScope.$$nextSibling;
}
}
返回计数;
}
使用对象作为返回值以获取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;
}
函数枚举子作用域(作用域)
{
//这是真的
var enum={};//排除当前范围
var root=scope | | angular.element(document.injector().get(“$rootScope”);
var pendingChildHeads=[root.$$childHead];
无功电流范围;
while(pendingChildHeads.长度)
{
currentScope=pendingChildHeads.shift();
while(当前范围)
{
枚举[“范围”+pendingChildHeads.length]=当前范围$id;
pendingChildHeads.push(当前范围$$childHead);
currentScope=currentScope.$$nextSibling;
}
}
返回枚举;
}
参考资料
=
符号的确切含义吗?@nh2,例如,假设你的HTML有
。如果指令指定了作用域:{myObj:'='}
,则父作用域和指令的隔离作用域都可以访问对象someObj
。在该指令中,您可以在链接函数中将其称为scope.myObj
。在指令上使用隔离作用域并通过“=”引用对象的问题是,您只能注册自己的作用域,其他由angular创建的作用域(如ng repeat作用域)将被忽略。另一种方法是不在指令上使用scope(默认为scope true,从父作用域继承),访问在父作用域创建的对象,您希望知道该对象的子作用域,然后继续执行相同的操作logic@HeberLZ这就是我最后做的。我建立了一个小部件工厂