Skip to content

Commit 159bbf1

Browse files
psysppetebacondarwin
authored andcommitted
docs($compile): improve documentation on directive $scope usage
Add information about the behavior of several directives, especially of their scopes when applied on a single element. Closes angular#5761 Closes angular#9727
1 parent 9d2cc83 commit 159bbf1

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/ng/compile.js

+29-10
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,23 @@
146146
* and other directives used in the directive's template will also be excluded from execution.
147147
*
148148
* #### `scope`
149-
* **If set to `true`,** then a new scope will be created for this directive. If multiple directives on the
150-
* same element request a new scope, only one new scope is created. The new scope rule does not
151-
* apply for the root of the template since the root of the template always gets a new scope.
149+
* The scope property can be `true`, an object or a falsy value:
152150
*
153-
* **If set to `{}` (object hash),** then a new "isolate" scope is created. The 'isolate' scope differs from
154-
* normal scope in that it does not prototypically inherit from the parent scope. This is useful
155-
* when creating reusable components, which should not accidentally read or modify data in the
156-
* parent scope.
151+
* * **falsy:** No scope will be created for the directive. The directive will use its parent's scope.
157152
*
158-
* The 'isolate' scope takes an object hash which defines a set of local scope properties
159-
* derived from the parent scope. These local properties are useful for aliasing values for
160-
* templates. Locals definition is a hash of local scope property to its source:
153+
* * **`true`:** A new scope will be created for the directive's element. If multiple directives on the
154+
* same element request a new scope, only one new scope is created. The new scope rule does not apply
155+
* for the root of the template since the root of the template always gets a new scope.
156+
*
157+
* * **`{...}` (an object hash):** A new "isolate" scope is created for the directive's element. The
158+
* 'isolate' scope differs from normal scope in that it does not prototypically inherit from its parent
159+
* scope. This is useful when creating reusable components, which should not accidentally read or modify
160+
* data in the parent scope.
161+
*
162+
* The 'isolate' scope object hash defines a set of local scope properties derived from attributes on the
163+
* directive's element. These local properties are useful for aliasing values for templates. The keys in
164+
* the object hash map to the name of the property on the isolate scope; the values define how the property
165+
* is bound to the parent scope, via matching attributes on the directive's element:
161166
*
162167
* * `@` or `@attr` - bind a local scope property to the value of DOM attribute. The result is
163168
* always a string since DOM attributes are strings. If no `attr` name is specified then the
@@ -190,6 +195,20 @@
190195
* For example, if the expression is `increment(amount)` then we can specify the amount value
191196
* by calling the `localFn` as `localFn({amount: 22})`.
192197
*
198+
* In general it's possible to apply more than one directive to one element, but there might be limitations
199+
* depending on the type of scope required by the directives. The following points will help explain these limitations.
200+
* For simplicity only two directives are taken into account, but it is also applicable for several directives:
201+
*
202+
* * **no scope** + **no scope** => Two directives which don't require their own scope will use their parent's scope
203+
* * **child scope** + **no scope** => Both directives will share one single child scope
204+
* * **child scope** + **child scope** => Both directives will share one single child scope
205+
* * **isolated scope** + **no scope** => The isolated directive will use it's own created isolated scope. The other directive will use
206+
* its parent's scope
207+
* * **isolated scope** + **child scope** => **Won't work!** Only one scope can be related to one element. Therefore these directives cannot
208+
* be applied to the same element.
209+
* * **isolated scope** + **isolated scope** => **Won't work!** Only one scope can be related to one element. Therefore these directives
210+
* cannot be applied to the same element.
211+
*
193212
*
194213
* #### `bindToController`
195214
* When an isolate scope is used for a component (see above), and `controllerAs` is used, `bindToController: true` will

0 commit comments

Comments
 (0)