Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
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

Description

@thorn0

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?

Activity

  1. gkalpak commented on Sep 8, 2016

    @gkalpak
    Member

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

  2. added this to the milestone on Sep 8, 2016
  3. Narretz commented on Sep 9, 2016

    @Narretz
    Contributor

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

  4. added 2 commits that reference this issue on Sep 9, 2016
    cc94831
    9f90f36
  5. added a commit that references this issue on Sep 10, 2016
    cdcd11e
  6. added 7 commits that reference this issue on Nov 21, 2016
    6c366e6
    0708192
    aab4399
    a932e13
    f68de1f
    22c3727
    00b01e0
  7. added a commit that references this issue on Sep 28, 2024
    d14c7f3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions