docs(API): 翻译了 ngIf

This commit is contained in:
Zhicheng Wang 2018-08-31 10:35:36 +08:00
parent 2cd125f40c
commit 46e428659a
1 changed files with 78 additions and 0 deletions

View File

@ -12,78 +12,153 @@ import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef, ɵstri
/** /**
* Conditionally includes a template based on the value of an `expression`. * Conditionally includes a template based on the value of an `expression`.
* *
* `expression`
*
* `ngIf` evaluates the `expression` and then renders the `then` or `else` template in its place * `ngIf` evaluates the `expression` and then renders the `then` or `else` template in its place
* when expression is truthy or falsy respectively. Typically the: * when expression is truthy or falsy respectively. Typically the:
*
* `ngIf` `expression` `then` `else`
*
* - `then` template is the inline template of `ngIf` unless bound to a different value. * - `then` template is the inline template of `ngIf` unless bound to a different value.
*
* `then` `ngIf`
*
* - `else` template is blank unless it is bound. * - `else` template is blank unless it is bound.
* *
* `else`
*
* ## Most common usage * ## Most common usage
* *
* ##
*
* The most common usage of the `ngIf` directive is to conditionally show the inline template as * The most common usage of the `ngIf` directive is to conditionally show the inline template as
* seen in this example: * seen in this example:
*
* `ngIf`
*
* {@example common/ngIf/ts/module.ts region='NgIfSimple'} * {@example common/ngIf/ts/module.ts region='NgIfSimple'}
* *
* ## Showing an alternative template using `else` * ## Showing an alternative template using `else`
* *
* ## `else`
*
* If it is necessary to display a template when the `expression` is falsy use the `else` template * If it is necessary to display a template when the `expression` is falsy use the `else` template
* binding as shown. Note that the `else` binding points to a `<ng-template>` labeled `#elseBlock`. * binding as shown. Note that the `else` binding points to a `<ng-template>` labeled `#elseBlock`.
* The template can be defined anywhere in the component view but is typically placed right after * The template can be defined anywhere in the component view but is typically placed right after
* `ngIf` for readability. * `ngIf` for readability.
* *
* `expression` `else`
* `else` `#elseBlock` `<ng-template>`
* `ngIf`
*
* {@example common/ngIf/ts/module.ts region='NgIfElse'} * {@example common/ngIf/ts/module.ts region='NgIfElse'}
* *
* ## Using non-inlined `then` template * ## Using non-inlined `then` template
* *
* ## 使 `then`
*
* Usually the `then` template is the inlined template of the `ngIf`, but it can be changed using * Usually the `then` template is the inlined template of the `ngIf`, but it can be changed using
* a binding (just like `else`). Because `then` and `else` are bindings, the template references can * a binding (just like `else`). Because `then` and `else` are bindings, the template references can
* change at runtime as shown in this example. * change at runtime as shown in this example.
* *
* `then` `ngIf` `else`
* `then` `else`
*
* {@example common/ngIf/ts/module.ts region='NgIfThenElse'} * {@example common/ngIf/ts/module.ts region='NgIfThenElse'}
* *
* ## Storing conditional result in a variable * ## Storing conditional result in a variable
* *
* ##
*
* A common pattern is that we need to show a set of properties from the same object. If the * A common pattern is that we need to show a set of properties from the same object. If the
* object is undefined, then we have to use the safe-traversal-operator `?.` to guard against * object is undefined, then we have to use the safe-traversal-operator `?.` to guard against
* dereferencing a `null` value. This is especially the case when waiting on async data such as * dereferencing a `null` value. This is especially the case when waiting on async data such as
* when using the `async` pipe as shown in following example: * when using the `async` pipe as shown in following example:
* *
* undefined使 `?.`
* 使 `async`
*
* ``` * ```
* Hello {{ (userStream|async)?.last }}, {{ (userStream|async)?.first }}! * Hello {{ (userStream|async)?.last }}, {{ (userStream|async)?.first }}!
* ``` * ```
* *
* There are several inefficiencies in the above example: * There are several inefficiencies in the above example:
*
*
*
* - We create multiple subscriptions on `userStream`. One for each `async` pipe, or two in the * - We create multiple subscriptions on `userStream`. One for each `async` pipe, or two in the
* example above. * example above.
*
* `userStream` `async`
*
* - We cannot display an alternative screen while waiting for the data to arrive asynchronously. * - We cannot display an alternative screen while waiting for the data to arrive asynchronously.
*
* loading
*
* - We have to use the safe-traversal-operator `?.` to access properties, which is cumbersome. * - We have to use the safe-traversal-operator `?.` to access properties, which is cumbersome.
*
* 使 `?.` 访
*
* - We have to place the `async` pipe in parenthesis. * - We have to place the `async` pipe in parenthesis.
* *
* `async`
*
* A better way to do this is to use `ngIf` and store the result of the condition in a local * A better way to do this is to use `ngIf` and store the result of the condition in a local
* variable as shown in the the example below: * variable as shown in the the example below:
* *
* 使 `ngIf`
*
* {@example common/ngIf/ts/module.ts region='NgIfAs'} * {@example common/ngIf/ts/module.ts region='NgIfAs'}
* *
* Notice that: * Notice that:
*
*
*
* - We use only one `async` pipe and hence only one subscription gets created. * - We use only one `async` pipe and hence only one subscription gets created.
*
* `async`
*
* - `ngIf` stores the result of the `userStream|async` in the local variable `user`. * - `ngIf` stores the result of the `userStream|async` in the local variable `user`.
*
* `ngIf` `userStream|async` `user`
*
* - The local `user` can then be bound repeatedly in a more efficient way. * - The local `user` can then be bound repeatedly in a more efficient way.
*
* `user`
*
* - No need to use the safe-traversal-operator `?.` to access properties as `ngIf` will only * - No need to use the safe-traversal-operator `?.` to access properties as `ngIf` will only
* display the data if `userStream` returns a value. * display the data if `userStream` returns a value.
*
* 使 `?.` 访 `ngIf` `userStream`
*
* - We can display an alternative template while waiting for the data. * - We can display an alternative template while waiting for the data.
* *
*
*
* ### Syntax * ### Syntax
* *
* ###
*
* Simple form: * Simple form:
*
*
*
* - `<div *ngIf="condition">...</div>` * - `<div *ngIf="condition">...</div>`
* - `<ng-template [ngIf]="condition"><div>...</div></ng-template>` * - `<ng-template [ngIf]="condition"><div>...</div></ng-template>`
* *
* Form with an else block: * Form with an else block:
*
* `else`
*
* ``` * ```
* <div *ngIf="condition; else elseBlock">...</div> * <div *ngIf="condition; else elseBlock">...</div>
* <ng-template #elseBlock>...</ng-template> * <ng-template #elseBlock>...</ng-template>
* ``` * ```
* *
* Form with a `then` and `else` block: * Form with a `then` and `else` block:
*
* `then` `else`
*
* ``` * ```
* <div *ngIf="condition; then thenBlock else elseBlock"></div> * <div *ngIf="condition; then thenBlock else elseBlock"></div>
* <ng-template #thenBlock>...</ng-template> * <ng-template #thenBlock>...</ng-template>
@ -91,6 +166,9 @@ import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef, ɵstri
* ``` * ```
* *
* Form with storing the value locally: * Form with storing the value locally:
*
*
*
* ``` * ```
* <div *ngIf="condition as value; else elseBlock">{{value}}</div> * <div *ngIf="condition as value; else elseBlock">{{value}}</div>
* <ng-template #elseBlock>...</ng-template> * <ng-template #elseBlock>...</ng-template>