rit*_*der 22 enumeration parent-child angularjs angularjs-scope
我想知道如何获得给定父范围的所有子范围的列表.我可以从范围的属性中找到所有内容:$$ childHead,$$ childTail,$$ nextSibling和$$ prevSibling.
我现在使用的方法是从父级获取childHead,然后使用nextSibling获取下一个子级,直到nextSibling为null.
有更好的方法吗?鉴于我想在所有孩子身上调用一个方法[getModel],还有更好的方法吗?
Azr*_*mil 25
所有Angular范围都附加到DOM元素,您可以通过使用当前元素检查子项到您想要访问的任何子项开始.在那里,使用下面的函数来获得范围.
angular.element('#5th_element').scope();
Run Code Online (Sandbox Code Playgroud)
Mar*_*cok 16
子指令使用隔离的范围,并且具有从父级看不到的自己的值.我想从父作用域访问这些值.
处理"需要访问子作用域的父作用域"问题的"Angular方法"是将模型移动到父作用域,并让子作用域引用父属性/数据(而不是具有自己的子作用域)当地财产/副本).例如,如果每个迭代包含一个输入表单元素(即每次迭代需要双向数据绑定),这就是我们处理ng-repeat的方式:ng-model,ng-repeat和输入的难度
使用指令,首先在父作用域中定义对象数组,然后让每个隔离的子作用域使用'='表示法访问父作用域数组(或单个对象)(即双向数据绑定表示法).由于正在共享对象,因此隔离的作用域将引用父对象(它们不会获得本地副本).现在,您对子作用域属性所做的任何更改实际上都在更改父作用域属性.
在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; }
参考
归档时间: |
|
查看次数: |
41673 次 |
最近记录: |