Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

$compile: "the root of the template always gets a new scope..." What is it about? #15109

Closed
thorn0 opened this issue Sep 8, 2016 · 2 comments

Comments

@thorn0
Copy link
Contributor

thorn0 commented Sep 8, 2016

In the docs for $compile, we can read the following:

The scope property can be true, an object or a falsy value: ...
true: A new child scope that prototypically inherits from its parent will be created for the directive's element. If multiple directives on the same element request a new scope, only one new scope is created. The new scope rule does not apply for the root of the template since the root of the template always gets a new scope.

I can't understand the last sentence. Does it mean that even if you set scope to false in the DDO, your directive will create a new scope if it has a template? Then, it's not true: see this plunk. Does this sentence mean something different?

@gkalpak
Copy link
Member

gkalpak commented Sep 8, 2016

I think this is a relic of the past and it basically refers to the root element of the application (which gets attached to $rootScope automatically.

It was introduced back in 4804c83 (v1.0.0-rc.2). At that time, it seems that the HTML inside index.html was referred to as "the angular template" or "DOM template" or just "template".

A few quotes from the (then) docs:

About compilation:

  1. At application compile time, a root scope is created and is attached to the root <HTML> DOM element.
  2. During the compilation phase, the compiler matches directives against the DOM template. The directives usually fall into one of two categories:
    • Observing directives, such as double-curly expressions {{expression}}, register listeners using the $watch() method. This type of directive needs to be notified whenever the expression changes so that it can update the view.
    • Listener directives, such as ng:click, register a listener with the DOM. When the DOM listener fires, the directive executes the associated expression and updates the view using the $apply() method.

About templates:

The following code snippet shows a simple angular template made up of standard HTML tags along with angular directives, markup, and expressions:

<html ng:app>
  <!-- Body tag augmented with ng:controller directive  -->
  <body ng:controller="MyController">
    <input ng:model="foo" value="bar">
    <!-- Button tag with ng:click directive, and string expression 'buttonText'
         wrapped in "{{ }}" markup -->
    <button ng:click="changeFoo()">{{buttonText}}</button>
    <script src="angular.js">
  </body>
</html>

In a simple single-page app, the template consists of HTML, CSS, and angular directives contained in just one HTML file (usually index.html). In a more complex app, you can display multiple views within one main page using "partials", which are segments of template located in separate HTML files.

Iff my analysis is correct, we can get rid of that sentence altogether as it is confusing (and doesn't seem related to directives anyway).

But I am just guessing here - there is always a chance it means something else...

@Narretz
Copy link
Contributor

Narretz commented Sep 9, 2016

I think we can remove it, as it doesn't really make anything clearer.

thorn0 added a commit to thorn0/angular.js that referenced this issue Sep 9, 2016
thorn0 added a commit to thorn0/angular.js that referenced this issue Sep 10, 2016
gkalpak pushed a commit that referenced this issue Sep 10, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this issue Nov 21, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this issue Nov 21, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this issue Nov 21, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this issue Nov 21, 2016
petebacondarwin pushed a commit to petebacondarwin/angular.js that referenced this issue Nov 21, 2016
petebacondarwin pushed a commit that referenced this issue Nov 23, 2016
petebacondarwin pushed a commit that referenced this issue Nov 24, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants