Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is ng-scope inserted only for new scopes?

Tags:

angularjs

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 ??

like image 713
PerrierCitror Avatar asked Dec 18 '13 23:12

PerrierCitror


People also ask

What does ng scope do?

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.

What is scope new?

$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.

What is the difference between the scopes of a directive and the scopes of a controller?

No difference. It is same object.

Can you explain the concept of scope hierarchy in angular?

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.


1 Answers

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>
like image 92
Davin Tryon Avatar answered Oct 12 '22 09:10

Davin Tryon