@@ -17,13 +17,14 @@ list of some of the possible directive names: `ng:bind`, `ng-bind`, `ng_bind`, `
17
17
`data-ng-bind`.
18
18
19
19
The directives can be placed in element names, attributes, class names, as well as comments. Here
20
- are some equivalent examples of invoking `ngBind`.
20
+ are some equivalent examples of invoking `myDir`. (However, most directives are restricted to
21
+ attribute only.)
21
22
22
23
<pre>
23
- <span ng-bind ="exp"></span>
24
- <span class="ng-bind : exp;"></span>
25
- <ng-bind ></ng-bind >
26
- <!-- directive: ng-bind exp --! >
24
+ <span my-dir ="exp"></span>
25
+ <span class="my-dir : exp;"></span>
26
+ <my-dir ></my-dir >
27
+ <!-- directive: my-dir exp -->
27
28
</pre>
28
29
29
30
Directives can be invoked in many different ways, but are equivalent in the end result as shown in
@@ -37,13 +38,12 @@ the following example.
37
38
}
38
39
</script>
39
40
<div ng-controller="Ctrl1">
40
- Hello <input ng-model='name'> <hr/>
41
+ Hello <input ng-model='name' ng-model-instant > <hr/>
41
42
<span ng:bind="name"> <span ng:bind="name"></span> <br/>
42
43
<span ng_bind="name"> <span ng_bind="name"></span> <br/>
43
44
<span ng-bind="name"> <span ng-bind="name"></span> <br/>
44
45
<span data-ng-bind="name"> <span data-ng-bind="name"></span> <br/>
45
46
<span x-ng-bind="name"> <span x-ng-bind="name"></span> <br/>
46
- <span class="ng-bind: name;"> <span class="ng-bind: name;"></span> <br/>
47
47
</div>
48
48
</doc:source>
49
49
<doc:scenario>
@@ -239,7 +239,7 @@ The full skeleton of the directive is shown here:
239
239
templateUrl: 'directive.html',
240
240
replace: false,
241
241
transclude: false,
242
- restrict: 'EACM ',
242
+ restrict: 'A ',
243
243
scope: false,
244
244
local: {},
245
245
compile: function compile(tElement, tAttrs, transclude) {
@@ -312,59 +312,66 @@ compiler}. The attributes are:
312
312
313
313
* `scope` - If set to:
314
314
315
- * `true` - then a new scope will be created for this directive. It is an error to have two
316
- directives on the same element both requesting new scope. The new scope rule does not apply
317
- for the root of the template since the root of the template always gets a new scope.
315
+ * `true` - then a new scope will be created for this directive. If multiple directives on the
316
+ same element request new scope, only one new scope is created . The new scope rule does not
317
+ apply for the root of the template since the root of the template always gets a new scope.
318
318
319
319
* `{}` (object hash) - then a new 'isolate' scope is created. The 'isolate' scope differs from
320
320
normal scope that it does not prototypically inherit from the parent scope. This is useful
321
- when creating reusable widgets , which should not accidentally read or modify data in parent
322
- scope. <br/>
323
- The 'isolate' scope takes an object hash which defines a set of local scope properties derived
324
- from the parent scope. These local properties are usefull for aliasing values for
321
+ when creating reusable components , which should not accidentally read or modify data in
322
+ parent scope. <br/>
323
+ The 'isolate' scope takes an object hash which defines a set of local scope properties
324
+ derived from the parent scope. These local properties are useful for aliasing values for
325
325
templates. Locals definition is a hash of normalized element attribute name to their
326
- coresponding binding strategy. Valid binding strategies are:
326
+ corresponding binding strategy. Valid binding strategies are:
327
327
328
328
* `attribute` - one time read of element attribute value and save it to widget scope. <br/>
329
- Given `<widget my-attr='abc'>` and widget definition of `locals: {myAttr:'attribute'}`, then
330
- widget scope property `myAttr` will be `"abc"`.
329
+ Given `<widget my-attr='abc'>` and widget definition of `locals: {myAttr:'attribute'}`,
330
+ then widget scope property `myAttr` will be `"abc"`.
331
331
332
- * `evaluate` - one time evaluation of expression stored in the attribute. <br/>
333
- Given `<widget my-attr='name'>` and widget definition of `locals: {myAttr:'evaluate'}`, and
332
+ * `evaluate` - one time evaluation of expression stored in the attribute. <br/> Given
333
+ `<widget my-attr='name'>` and widget definition of `locals: {myAttr:'evaluate'}`, and
334
334
parent scope `{name:'angular'}` then widget scope property `myAttr` will be `"angular"`.
335
335
336
336
* `bind` - Set up one way binding from the element attribute to the widget scope. <br/>
337
- Given `<widget my-attr='{{name}}'>` and widget definition of `locals: {myAttr:'bind'}`, and
338
- parent scope `{name:'angular'}` then widget scope property `myAttr` will be `"angular"`, but
339
- any changes in the parent scope will be reflected in the widget scope.
340
-
341
- * `accessor` - Set up getter/setter function for the expression in the widget element attribute
342
- to the widget scope. <br/>
343
- Given `<widget my-attr='name'>` and widget definition of `locals: {myAttr:'prop'}`, and
344
- parent scope `{name:'angular'}` then widget scope property `myAttr` will be a function such
345
- that `myAttr()` will return `"angular"` and `myAttr('new value')` will update the parent
346
- scope `name` property. This is usefull for treating the element as a data-model for
347
- reading/writing.
348
-
349
- * `expression` - Treat element attribute as an expression to be exectude in form of an event.
337
+ Given `<widget my-attr='{{name}}'>` and widget definition of `locals: {myAttr:'bind'}`,
338
+ and parent scope `{name:'angular'}` then widget scope property `myAttr` will be
339
+ `"angular"`, but any changes in the parent scope will be reflected in the widget scope.
340
+
341
+ * `accessor` - Set up getter/setter function for the expression in the widget element
342
+ attribute to the widget scope. <br/> Given `<widget my-attr='name'>` and widget definition
343
+ of `locals: {myAttr:'prop'}`, and parent scope `{name:'angular'}` then widget scope
344
+ property `myAttr` will be a function such that `myAttr()` will return `"angular"` and
345
+ `myAttr('new value')` will update the parent scope `name` property. This is useful for
346
+ treating the element as a data-model for reading/writing.
347
+
348
+ * `expression` - Treat element attribute as an expression to be executed in form of an event.
350
349
<br/>
351
- Given `<widget my-attr='doSomething()'>` and widget definition of
352
- `locals: {myAttr:'expression'}`, and parent scope `{doSomething:function() {}}` then calling
353
- the widget scope function `myAttr` will execute the expression against the parent scope.
350
+ Given `<widget my-attr='doSomething()'>` and widget definition of `locals:
351
+ {myAttr:'expression'}`, and parent scope `{doSomething:function() {}}` then calling the
352
+ widget scope function `myAttr` will execute the expression against the parent scope.
354
353
355
354
* `controller` - Controller constructor function. The controller is instantiated before the
356
- pre-linking phase and it is shared with directives, if they request it by name. This allows the
357
- directives to communicate with each other and augment each other behavior. The controller is
358
- injectable with the following locals:
355
+ pre-linking phase and it is shared with other directives if they request it by name (see
356
+ `require` attribute). This allows the directives to communicate with each other and augment
357
+ each other behavior. The controller is injectable with the following locals:
359
358
360
359
* `$scope` - Current scope associated with the element
361
360
* `$element` - Current element
362
361
* `$attrs` - Current attributes obeject for the element
363
362
* `$transclude` - A transclude linking function pre-bound to the correct transclusion scope:
364
363
`function(cloneLinkingFn)`.
365
364
365
+ * `require` - Require another controller be passed into current directive linking function. The
366
+ `require` takes a name of the directive controller to pass in. If no such controller can be
367
+ found an error is raised. The name can be prefixed with:
368
+
369
+ * `?` - Don't raise an error. This makes the require dependency optional.
370
+ * `^` - Look for the controller on parent elements as well.
371
+
372
+
366
373
* `inject` (object hash) - Specifies a way to inject bindings into a controller. Injection
367
- definition is a hash of normalized element attribute name to their coresponding binding
374
+ definition is a hash of normalized element attribute name to their corresponding binding
368
375
strategy. Valid binding strategies are:
369
376
370
377
* `attribute` - inject attribute value. <br/>
@@ -389,16 +396,8 @@ compiler}. The attributes are:
389
396
injecting `myAttr` will inject a function which when called will execute the expression
390
397
against the parent scope.
391
398
392
- * `require` - Require the another controller be passed into current directive linking function.
393
- The `require` takes a name of the directive controller to pass in. If no such controller
394
- can be found an error is raised. The name can be prefixd with:
395
-
396
- * `?` - Don't reaise an error. This makes the require dependency optional.
397
- * `^` - Look for the controller on parent elements as well.
398
-
399
-
400
399
* `restrict` - String of subset of `EACM` which restricts the directive to a specific directive
401
- declaration style.
400
+ declaration style. If omitted directives are allowed on attributes only.
402
401
403
402
* `E` - Element name: `<my-directive></my-directive>`
404
403
* `A` - Attribute: `<div my-directive="exp"></div>`
@@ -534,8 +533,8 @@ function linkingFn(scope, elm, attrs, ctrl) {
534
533
535
534
# Understanding Transclusion and Scopes
536
535
537
- It is often desirable to have reusable components, which we will refer to as widgets. Below is a
538
- pseudo code showing how a simplified dialog widget may work.
536
+ It is often desirable to have reusable components. Below is a pseudo code showing how a simplified
537
+ dialog component may work.
539
538
540
539
<pre>
541
540
<div>
@@ -570,7 +569,9 @@ This will not render properly, unless we do some scope magic.
570
569
The first issue we have to solve is that the dialog box template expect `title` to be defined, but
571
570
the place of instantiation would like to bind to `username`. Furthermore the buttons expect `onOk`
572
571
as well as `onCancel` functions to be present in the scope. This limits the usefulness of the
573
- widget. To solve the mapping issue we use the `locals` to create local variables which the template expects as follows
572
+ widget. To solve the mapping issue we use the `locals` to create local variables which the
573
+ template expects as follows
574
+
574
575
<pre>
575
576
locals: {
576
577
title: 'bind', // set up title to accept data-binding
@@ -606,16 +607,15 @@ Therefore the final directive definition looks something like this:
606
607
607
608
<pre>
608
609
transclude: true,
609
- scope: 'isolate',
610
- locals: {
610
+ scope: {
611
611
title: 'bind', // set up title to accept data-binding
612
612
onOk: 'exp', // create a delegate onOk function
613
613
onCancel: 'exp', // create a delegate onCancel function
614
614
show: 'prop' // create a getter/setter function for visibility.
615
615
}
616
616
</pre>
617
617
618
- # Creating Widgets
618
+ # Creating Components
619
619
620
620
It is often desirable to replace a single directive with a more complex DOM structure. This
621
621
allows the directives to become a short hand for reusable components from which applications
@@ -635,6 +635,7 @@ Following is an example of building a reusable widget.
635
635
angular.module('zippyModule', [])
636
636
.directive('zippy', function(){
637
637
return {
638
+ restrict: 'C',
638
639
// This HTML will replace the zippy directive.
639
640
replace: true,
640
641
transclude: true,
0 commit comments