Skip to content

Commit 857bef9

Browse files
committed
doc(NgSwitch): update API doc
Closes angular#4423
1 parent 4e585bc commit 857bef9

1 file changed

Lines changed: 59 additions & 38 deletions

File tree

modules/angular2/src/common/directives/ng_switch.ts

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ListWrapper, Map} from 'angular2/src/facade/collection';
66

77
const _WHEN_DEFAULT = CONST_EXPR(new Object());
88

9-
export class SwitchView {
9+
class SwitchView {
1010
constructor(private _viewContainerRef: ViewContainerRef, private _templateRef: TemplateRef) {}
1111

1212
create(): void { this._viewContainerRef.createEmbeddedView(this._templateRef); }
@@ -15,28 +15,59 @@ export class SwitchView {
1515
}
1616

1717
/**
18-
* The `NgSwitch` directive is used to conditionally swap DOM structure on your template based on a
19-
* scope expression.
18+
* Adds or removes DOM sub-trees when their match expressions match the switch expression.
19+
*
2020
* Elements within `NgSwitch` but without `NgSwitchWhen` or `NgSwitchDefault` directives will be
2121
* preserved at the location as specified in the template.
2222
*
23-
* `NgSwitch` simply chooses nested elements and makes them visible based on which element matches
24-
* the value obtained from the evaluated expression. In other words, you define a container element
25-
* (where you place the directive), place an expression on the **`[ng-switch]="..."` attribute**),
26-
* define any inner elements inside of the directive and place a `[ng-switch-when]` attribute per
27-
* element.
28-
* The when attribute is used to inform NgSwitch which element to display when the expression is
29-
* evaluated. If a matching expression is not found via a when attribute then an element with the
30-
* default attribute is displayed.
23+
* `NgSwitch` simply inserts nested elements based on which match expression matches the value
24+
* obtained from the evaluated switch expression. In other words, you define a container element
25+
* (where you place the directive with a switch expression on the
26+
* **`[ng-switch]="..."` attribute**), define any inner elements inside of the directive and
27+
* place a `[ng-switch-when]` attribute per element.
3128
*
32-
* ### Example
29+
* The `ng-switch-when` property is used to inform `NgSwitch` which element to display when the
30+
* expression is evaluated. If a matching expression is not found via a `ng-switch-when` property
31+
* then an element with the `ng-switch-default` attribute is displayed.
3332
*
34-
* ```
35-
* <ANY [ng-switch]="expression">
36-
* <template [ng-switch-when]="whenExpression1">...</template>
37-
* <template [ng-switch-when]="whenExpression1">...</template>
38-
* <template ng-switch-default>...</template>
39-
* </ANY>
33+
* ### Example ([live demo](http://plnkr.co/edit/DQMTII95CbuqWrl3lYAs?p=preview))
34+
*
35+
* ```typescript
36+
* @Component({selector: 'app'})
37+
* @View({
38+
* template: `
39+
* <p>Value = {{value}}</p>
40+
* <button (click)="inc()">Increment</button>
41+
*
42+
* <div [ng-switch]="value">
43+
* <p *ng-switch-when="'init'">increment to start</p>
44+
* <p *ng-switch-when="0">0, increment again</p>
45+
* <p *ng-switch-when="1">1, increment again</p>
46+
* <p *ng-switch-when="2">2, stop incrementing</p>
47+
* <p *ng-switch-default>&gt; 2, STOP!</p>
48+
* </div>
49+
*
50+
* <!-- alternate syntax -->
51+
*
52+
* <p [ng-switch]="value">
53+
* <template ng-switch-when="init">increment to start</template>
54+
* <template [ng-switch-when]="0">0, increment again</template>
55+
* <template [ng-switch-when]="1">1, increment again</template>
56+
* <template [ng-switch-when]="2">2, stop incrementing</template>
57+
* <template ng-switch-default>&gt; 2, STOP!</template>
58+
* </p>
59+
* `,
60+
* directives: [NgSwitch, NgSwitchWhen, NgSwitchDefault]
61+
* })
62+
* export class App {
63+
* value = 'init';
64+
*
65+
* inc() {
66+
* this.value = this.value === 'init' ? 0 : this.value + 1;
67+
* }
68+
* }
69+
*
70+
* bootstrap(App).catch(err => console.error(err));
4071
* ```
4172
*/
4273
@Directive({selector: '[ng-switch]', inputs: ['ngSwitch']})
@@ -130,19 +161,12 @@ export class NgSwitch {
130161
}
131162

132163
/**
133-
* Defines a case statement as an expression.
164+
* Insert the sub-tree when the `ng-switch-when` expression evaluates to the same value as the
165+
* enclosing switch expression.
134166
*
135-
* If multiple `NgSwitchWhen` match the `NgSwitch` value, all of them are displayed.
167+
* If multiple match expression match the switch expression value, all of them are displayed.
136168
*
137-
* Example:
138-
*
139-
* ```
140-
* // match against a context variable
141-
* <template [ng-switch-when]="contextVariable">...</template>
142-
*
143-
* // match against a constant string
144-
* <template ng-switch-when="stringValue">...</template>
145-
* ```
169+
* See {@link NgSwitch} for more details and example.
146170
*/
147171
@Directive({selector: '[ng-switch-when]', inputs: ['ngSwitchWhen']})
148172
export class NgSwitchWhen {
@@ -151,9 +175,11 @@ export class NgSwitchWhen {
151175
_value: any = _WHEN_DEFAULT;
152176
/** @internal */
153177
_view: SwitchView;
178+
private _switch: NgSwitch;
154179

155180
constructor(viewContainer: ViewContainerRef, templateRef: TemplateRef,
156-
@Host() private _switch: NgSwitch) {
181+
@Host() ngSwitch: NgSwitch) {
182+
this._switch = ngSwitch;
157183
this._view = new SwitchView(viewContainer, templateRef);
158184
}
159185

@@ -164,15 +190,10 @@ export class NgSwitchWhen {
164190
}
165191

166192
/**
167-
* Defines a default case statement.
193+
* Default case statements are displayed when no match expression matches the switch expression
194+
* value.
168195
*
169-
* Default case statements are displayed when no `NgSwitchWhen` match the `ng-switch` value.
170-
*
171-
* Example:
172-
*
173-
* ```
174-
* <template ng-switch-default>...</template>
175-
* ```
196+
* See {@link NgSwitch} for more details and example.
176197
*/
177198
@Directive({selector: '[ng-switch-default]'})
178199
export class NgSwitchDefault {

0 commit comments

Comments
 (0)