Why $ ChildScope. $ Parent. $ Parent. $ Root. $ Root ... is coiled?

I am new to angular / js and am trying to build an app using Sails.

When I created index.html, whose controller I was using to print the $ scope value to the console, I saw the attached screenshot.

<div class="container" ng-controller='angHomeController'>

      

And here is my controller

angular.module('myApp').controller('angHomeController', function($scope, $location) {
  console.log($scope);
});

      

And this is what I see on the console. enter image description here

Of the many questions that have popped into my head, a few are below.

Before I start developing / creating / using more variables / closures / callbacks of the template controller, I would like to understand a bit about this.

Q. # 1. The depth of the tree in this case is $ ChildScope. $ Parent. $ Parent (which is inherited from Scope). I see $ ChildScope. $ Parent. $ Parent. $$ listeners as locationChangeStart, locationChangeSuccess and routeChangeSuccess arrays with their proto (s). May I assume that this parent is $ parent. Is $ Parent created in Sails MVC Framework using angular's $ routeProvider API?

Q. # 2. I would prefer the top of the tree, $ ChildScope, created by the ng controller on $ rootScope. $ New. But I'm still intrigued why the $ ChildScope tree. $ Parent. $ Parent instead of $ ChildScope. $ Parent? Am I missing something? The parent has $ ChildScope. $ No observers or listeners. ChildScope has no listeners, of course, since I haven't created any events.

Q. # 3. I also noticed that $ ChildScope. $ Parent doesn't have $ root, but $ ChildScope. $ Parent. $ Parent. $ Root exists and appears to be a link to $ ChildScope. $ Parent. $ Parent. Also, $ ChildScope. $ Parent. $ Parent. $ Root. $ Root ... is spiral and does not end. The last $ parent link is $ root, which has $ root, and so on. Why doesn't this root chain exist and where does it come from for a link?

+3


source to share


1 answer


Angular scopes 'mimic' DOM structure of the application.

Scopes are created for the Angular app, each Angular controller, and some directives (if you specify that the directive should create or reuse a scope).



Playing with $ scope. $ parent. $ parent is not recommended as it is very fragile. If you change your Angular controller or any directive around the DOM tree, your scope hierarchy will be affected.

I would recommend reading up on What Are the Application Areas on the Angular Website.

0


source







All Articles