I would like to know if there is a way to access the $scopes variables using the $rootScope such that if a function is defined in scope I can invoke it using $rootscope to or if there is a variable defined using $scope.var1 I could access it using $rootScope to
Asked
Active
Viewed 3,604 times
5
-
`$scope` is child of `$rootScope` simply they maintain inheritance relation between them..and you know parent can not have access to its child...as child can access its parent info..because its generalize.. – Pankaj Parkar Sep 09 '15 at 16:12
-
@PankajParkar Actually parent has access to its child scope. So it is possible, however not very useful. – dfsq Sep 09 '15 at 16:14
-
@dfsq Thanks. curious to know how can we do that..? – Pankaj Parkar Sep 09 '15 at 16:15
-
@dfsq how is it possible and why is it not helpful – Gardezi Sep 09 '15 at 16:15
-
@james Because you don't want to do it normally. I can't image properly designed code that needs it. – dfsq Sep 09 '15 at 16:16
-
@dfsq is there any link or something from where I can get an idea of how to do it – Gardezi Sep 09 '15 at 16:17
-
Okay, I will post an answer if you really want to know it. – dfsq Sep 09 '15 at 16:20
-
@dfsq sure dsfq..you should do that :) – Pankaj Parkar Sep 09 '15 at 16:21
-
3"Sure, I can show you how to drive your car off the road real fast & nice!" – Mikko Viitala Sep 09 '15 at 16:23
-
1There's no reason for $rootScope to access his childscopes. You should really try to avoid this. – Leo Sep 09 '15 at 16:25
-
Exactly, @Leo is correct. As I said, in properly designed application you will never need this. – dfsq Sep 09 '15 at 16:26
1 Answers
5
Every scope have a two property references to its child scopes, namely: $$childHead
and $$childTail
. Additionally every scope object has $$nextSibling
and $$prevSibling
properties pointing to same-level scope sibling instances. Having this properties you can travers all child scopes horizontally or vertically. Depending why you need to do it on each step you would check for necessary scope property or method.
That being said, I can't see real business-logic application for such child scope traversal, except for logging/debugging purposes, for example to build scope hierarchy tree, etc.

dfsq
- 191,768
- 25
- 236
- 258
-
-
-
If you are confused do a console.log($rootScope) and look in the developer console. Find the $$childHead and $$nextSibling nodes and look through them. – mbokil Apr 05 '17 at 00:18