5

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

11
  • $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.. Commented Sep 9, 2015 at 16:12
  • @PankajParkar Actually parent has access to its child scope. So it is possible, however not very useful. Commented Sep 9, 2015 at 16:14
  • @dfsq Thanks. curious to know how can we do that..? Commented Sep 9, 2015 at 16:15
  • 3
    "Sure, I can show you how to drive your car off the road real fast & nice!" Commented Sep 9, 2015 at 16:23
  • 1
    There's no reason for $rootScope to access his childscopes. You should really try to avoid this. Commented Sep 9, 2015 at 16:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried to access the child scope but I'm not able to that
I did it like this $rootScope.$$childHead but I get null in return
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.