Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

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.

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?

like image 520
ritcoder Avatar asked Sep 29 '12 01:09

ritcoder


People also ask

How do you access child controller scope in parent controller?

In a nutshell: You cannot access child scopes from a parent scope. Your solutions: Define properties in parents and access them from children (read the link above) Use a service to share state.

How many child scopes can an AngularJS application have?

Scope Hierarchies Every angularJS application can have only one root scope as it is the parent scope. But it can have more than one child scope. New scopes can be created by directives which are added to parent scope as child scope.

What is parent scope in AngularJS?

Angular scopes include a variable called $parent (i.e. $scope. $parent ) that refer to the parent scope of a controller. If a controller is at the root of the application, the parent would be the root scope ( $rootScope ). Child controllers can therefore modify the parent scope since they access to it.

How does scope inheritance work in AngularJS?

Scope InheritanceIf we define nested controllers, then the child controller inherits the scope of its parent controller. We assign values to the models in shapeController. We override message in child controller named circleController.


1 Answers

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(); 
like image 85
Azri Jamil Avatar answered Sep 28 '22 22:09

Azri Jamil