This may sound kinda dumb .. But I have problems understanding if the 'ng-scope' class is inserted only when a new scope is created, or is it something else ?
Example : I have these lines of code linked to a controller :
<button class="btn" ng-click="open()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
In the web console, both have an ng-scope :
<button class="btn ng-scope" ng-click="open()">Open me!</button>
<div ng-show="selected" class="ng-scope ng-binding ng-hide">Selection from a modal: </div>
Even when there is no angular-specific data, like here, it will add an ng-scope :
<div>hello</div>
outputs
<div class="ng-scope">hello</div>
But why ??
The Scope in AngularJS is the binding part between HTML (view) and JavaScript (controller) and it is a built-in object. It contains application data and objects. It is available for both the view and the controller.
$scope. $new(true) creates a new child scope that does not prototypically inherit from a parent scope - i.e. it is an isolate scope that does "see" properties defined in the parent scope.
No difference. It is same object.
Scope hierarchyThe root scope is created whenever the Angular application is created, but then, directives cam create new child scopes. When a new child scope is created it is added as a child of his parent scope. This tree of child scopes normally parallels the DOM where they're attached.
Any place there is a scope attached. From the documentation:
Notice that Angular automatically places ng-scope class on elements where scopes are attached. The definition in this example highlights in red the new scope locations. The child scopes are necessary because the repeater evaluates {{name}} expression, but depending on which scope the expression is evaluated it produces different result.
And in this answer, @MarkRajcok indicates that angular uses these to track scopes (and garbage collect).
EDIT:
And to answer your edited question. No, this does not add an ng-scope
class:
<div>hello</div>
Here is a plunker to see this in action.
Notice how ng-scope
class is only applied to the node where ng-controller
is declared.
<div ng-controller="Ctrl" class="ng-scope">
<div>hello2</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With