Get to get all child scopes in Angularjs given the parent scope

社会主义新天地 提交于 2019-11-28 21:15:01
Mark Rajcok

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.

All Angular scope are attached to DOM element, you can start by inspecting child using the current element down to whatever child you want to reach. Once there, use below function to get the scope.

angular.element('#5th_element').scope();

In AngularJS 1.3.2, a countChildScopes method was added to the ngMock module:

/**
* @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;
  }

Use an object as a return value to get the IDs:

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;
  }

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!