Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e61a86

Browse files
vandycknickmhevery
authored andcommitted
docs: annotations
1 parent 1c9938e commit 0e61a86

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

modules/angular2/docs/core/01_templates.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ Example:
187187
<pre>
188188
```
189189
<ul>
190-
<li template="foreach: #item in items">
190+
<li template="for: #item of items">
191191
{{item}}
192192
</li>
193193
</ul>
@@ -201,8 +201,8 @@ Example:
201201
<pre>
202202
```
203203
<ul>
204-
<template def-foreach:"item"
205-
bind-foreach-in="items">
204+
<template def-for:"item"
205+
bind-for-in="items">
206206
<li>
207207
{{item}}
208208
</li>
@@ -221,8 +221,8 @@ Example:
221221

222222
<pre>
223223
```
224-
<template #foreach="item"
225-
[foreach-in]="items">
224+
<template #for="item"
225+
[for-in]="items">
226226
_some_content_to_repeat_
227227
</template>
228228
```
@@ -234,8 +234,8 @@ Example:
234234
Example:
235235
<pre>
236236
```
237-
<template def-foreach="item"
238-
bind-foreach-in="items">
237+
<template def-for="item"
238+
bind-for-in="items">
239239
_some_content_to_repeat_
240240
</template>
241241
```
@@ -408,30 +408,30 @@ NOTE: Only Viewport directives can be placed on the template element. (Decorator
408408
### Template Microsyntax
409409

410410
Often times it is necessary to encode a lot of different bindings into a template to control how the instantiation
411-
of the templates occurs. One such example is `foreach`.
411+
of the templates occurs. One such example is `for`.
412412

413413
```
414414
<form #foo=form>
415415
</form>
416416
<ul>
417-
<template foreach #person [in]="people" #i="index">
417+
<template for #person [in]="people" #i="index">
418418
<li>{{i}}. {{person}}<li>
419419
</template>
420420
</ul>
421421
```
422422

423423
Where:
424-
* `foreach` triggers the foreach directive.
425-
* `[in]="people"` binds an iterable object to the `foreach` controller.
426-
* `#person` exports the implicit `foreach` item.
424+
* `for` triggers the for directive.
425+
* `[in]="people"` binds an iterable object to the `for` controller.
426+
* `#person` exports the implicit `for` item.
427427
* `#i=index` exports item index as `i`.
428428

429429
The above example is explicit but quite wordy. For this reason in most situations a short hand version of the
430430
syntax is preferable.
431431

432432
```
433433
<ul>
434-
<li template="foreach; #person; in=people; #i=index;">{{i}}. {{person}}<li>
434+
<li template="for; #person; in=people; #i=index;">{{i}}. {{person}}<li>
435435
</ul>
436436
```
437437

@@ -441,19 +441,28 @@ which allows us to further shorten the text.
441441

442442
```
443443
<ul>
444-
<li template="foreach #person in people #i=index">{{i}}. {{person}}<li>
444+
<li template="for #person of people #i=index">{{i}}. {{person}}<li>
445445
</ul>
446446
```
447447

448-
We can also optionally use `var` instead of `#` and add `:` to `foreach` which creates the following recommended
449-
microsyntax for `foreach`.
448+
We can also optionally use `var` instead of `#` and add `:` to `for` which creates the following recommended
449+
microsyntax for `for`.
450450

451451
```
452452
<ul>
453-
<li template="foreach: var person in people; var i=index">{{i}}. {{person}}<li>
453+
<li template="for: var person of people; var i=index">{{i}}. {{person}}<li>
454454
</ul>
455455
```
456456

457+
Finally, we can move the `for` keyword to the left hand side and prefix it with `*` as so:
458+
459+
```
460+
<ul>
461+
<li *for="var person of people; var i=index">{{i}}. {{person}}<li>
462+
</ul>
463+
```
464+
465+
457466
The format is intentionally defined freely, so that developers of directives can build an expressive microsyntax for
458467
their directives. The following code describes a more formal definition.
459468

modules/angular2/src/core/annotations/annotations.js

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ export class Component extends Directive {
556556
}
557557

558558
/**
559-
* A directive used for dynamically loading components.
559+
* Directive used for dynamically loading components.
560560
*
561561
* Regular angular components are statically resolved. DynamicComponent allows to you resolve a component at runtime
562562
* instead by providing a placeholder into which a regular angular component can be dynamically loaded. Once loaded,
@@ -637,9 +637,9 @@ export class DynamicComponent extends Directive {
637637
/**
638638
* Directive that attaches behavior to DOM elements.
639639
*
640-
* A decorator directive attaches behavior to a DOM element in a composable manner.
640+
* A decorator directive attaches behavior to a DOM element in a composable manner.
641641
* (see: http://en.wikipedia.org/wiki/Composition_over_inheritance)
642-
*
642+
*
643643
* Decorators:
644644
* - are simplest form of [Directive]s.
645645
* - are best used as a composition pattern ()
@@ -652,7 +652,7 @@ export class DynamicComponent extends Directive {
652652
*
653653
* ## Example
654654
*
655-
* Here we use a decorator directive to simply define basic tool-tip behavior.
655+
* Here we use a decorator directive to simply define basic tool-tip behavior.
656656
*
657657
* ```
658658
* @Decorator({
@@ -685,9 +685,9 @@ export class DynamicComponent extends Directive {
685685
* }
686686
* }
687687
* ```
688-
* In our HTML template, we can then add this behavior to a `<div>` or any other element with the `tooltip` selector,
688+
* In our HTML template, we can then add this behavior to a `<div>` or any other element with the `tooltip` selector,
689689
* like so:
690-
*
690+
*
691691
* ```
692692
* <div tooltip="some text here"></div>
693693
* ```
@@ -728,24 +728,45 @@ export class Decorator extends Directive {
728728
}
729729

730730
/**
731-
* Viewport is used for controlling the instatiation of inline templates. [TODO: needs co-work :)]
731+
* Directive that controls the instantiation, destruction, and positioning of inline template elements.
732732
*
733-
* Viewport consist of a controller which can inject [ViewContainer]. A [ViewContainer] represents a location in the
734-
* current view where child views can be inserted. [ViewContainer] is created as a result of `<template>` element.
733+
* A viewport directive uses a [ViewContainer] to instantiate, insert, move, and destroy views at runtime.
734+
* The [ViewContainer] is created as a result of `<template>` element, and represents a location in the current view
735+
* where these actions are performed.
735736
*
736-
* NOTE: `<template>` directives can be created in shorthand form as `<TAG template="...">` or `<TAG *key="...">`
737+
* Views are always created as children of the current [View], and as siblings of the `<template>` element. Thus a
738+
* directive in a child view cannot inject the viewport directive that created it.
737739
*
738-
* ## Example
740+
* Since viewport directives are common in Angular, and using the full `<template>` element syntax is wordy, Angular
741+
* also supports a shorthand notation: `<li *foo="bar">` and `<li template="foo: bar">` are equivalent.
739742
*
740-
* Given folowing inline template, let's implement the `unless` behavior.
743+
* Thus,
741744
*
742745
* ```
743746
* <ul>
744-
* <li *unless="expr"></li>
747+
* <li *foo="bar" title="text"></li>
748+
* </ul>
749+
* ```
750+
*
751+
* Expands in use to:
752+
*
753+
* ```
754+
* <ul>
755+
* <template [foo]="bar">
756+
* <li title="text"></li>
757+
* </template>
745758
* </ul>
746759
* ```
747760
*
748-
* Can be implemented using:
761+
* Notice that although the shorthand places `*foo="bar"` within the `<li>` element, the binding for the `Viewport`
762+
* controller is correctly instantiated on the `<template>` element rather than the `<li>` element.
763+
*
764+
*
765+
* ## Example
766+
*
767+
* Let's suppose we want to implement the `unless` behavior, to conditionally include a template.
768+
*
769+
* Here is a simple viewport directive that triggers on an `unless` selector:
749770
*
750771
* ```
751772
* @Viewport({
@@ -754,7 +775,7 @@ export class Decorator extends Directive {
754775
* 'condition': 'unless'
755776
* }
756777
* })
757-
* export class If {
778+
* export class Unless {
758779
* viewContainer: ViewContainer;
759780
* prevCondition: boolean;
760781
*
@@ -775,20 +796,14 @@ export class Decorator extends Directive {
775796
* }
776797
* ```
777798
*
778-
* To better undarstand what is hapening, remember that angular converts the above template to:
779-
*
799+
* We can then use this `unless` selector in a template:
780800
* ```
781801
* <ul>
782-
* <template [unless]="exp">
783-
* <li></li>
784-
* </template>
802+
* <li *unless="expr"></li>
785803
* </ul>
786804
* ```
787805
*
788-
* Notice that the `*unless="exp"` got hoisted to `<template>`. This means that the `Viewport` controller is instantiated
789-
* on the `<template>` element rather thna the `<li>` element.
790-
*
791-
* Once the viewport insntantiates the child view the result is:
806+
* Once the viewport instantiates the child view, the shorthand notation for the template expands and the result is:
792807
*
793808
* ```
794809
* <ul>
@@ -799,9 +814,8 @@ export class Decorator extends Directive {
799814
* </ul>
800815
* ```
801816
*
802-
* The key thing to notice here is that `<li>` instance is a sibling of `<template>` not a child. For this reason
803-
* it is not possible to inject `Viewport` directive into other directives, (`Viweport` directives are always on
804-
* `<template>` elements which are leafs.)
817+
* Note also that although the `<li></li>` template still exists inside the `<template></template>`, the instantiated
818+
* view occurs on the second `<li></li>` which is a sibling to the `<template>` element.
805819
*
806820
*
807821
* @publicModule angular2/annotations
@@ -831,7 +845,7 @@ export class Viewport extends Directive {
831845
//TODO(misko): turn into LifecycleEvent class once we switch to TypeScript;
832846

833847
/**
834-
* Specify that a directive should be notified whenever a [View] that contains it is destroyed.
848+
* Notify a directive whenever a [View] that contains it is destroyed.
835849
*
836850
* ## Example
837851
*
@@ -852,7 +866,7 @@ export const onDestroy = "onDestroy";
852866

853867

854868
/**
855-
* Specify that a directive should be notified when any of its bindings have changed.
869+
* Notify a directive when any of its bindings have changed.
856870
*
857871
* ## Example:
858872
*

0 commit comments

Comments
 (0)