From b4db73d0bf0bdb0a15f71a9ac6f69284f5c2686a Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 7 Dec 2016 21:41:27 -0800 Subject: [PATCH] feat: ngIf now supports else; saves condition to local var NgIf syntax has been extended to support else clause to display template when the condition is false. In addition the condition value can now be stored in local variable, for later reuse. This is especially useful when used with the `async` pipe. Example: ```
Hello {{user.last}}, {{user.first}}!
``` closes #13061 closes #13297 --- .../@angular/common/src/directives/ng_if.ts | 144 +++++++++++++++--- .../common/test/directives/ng_if_spec.ts | 72 +++++++++ .../common/ngIf/ts/e2e_test/ngIf_spec.ts | 72 +++++++++ .../examples/common/ngIf/ts/module.ts | 128 ++++++++++++++++ .../language-service/test/test_data.ts | 2 +- .../language-service/test/ts_plugin_spec.ts | 2 +- tools/public_api_guard/common/index.d.ts | 4 +- 7 files changed, 402 insertions(+), 22 deletions(-) create mode 100644 modules/@angular/examples/common/ngIf/ts/e2e_test/ngIf_spec.ts create mode 100644 modules/@angular/examples/common/ngIf/ts/module.ts diff --git a/modules/@angular/common/src/directives/ng_if.ts b/modules/@angular/common/src/directives/ng_if.ts index 9d8f42f36a..ddecc54d06 100644 --- a/modules/@angular/common/src/directives/ng_if.ts +++ b/modules/@angular/common/src/directives/ng_if.ts @@ -6,46 +6,152 @@ * found in the LICENSE file at https://angular.io/license */ -import {Directive, Input, TemplateRef, ViewContainerRef} from '@angular/core'; +import {Directive, EmbeddedViewRef, Input, TemplateRef, ViewContainerRef} from '@angular/core'; + /** - * Removes or recreates a portion of the DOM tree based on an {expression}. + * Conditionally includes a template based on the value of an `expression`. * - * If the expression assigned to `ngIf` evaluates to a falsy value then the element - * is removed from the DOM, otherwise a clone of the element is reinserted into the DOM. + * `ngIf` evaluates the `expression` and then renders the `then` or `else` template in its place + * when expression is thruthy or falsy respectively. Typically the: + * - `then` template is the inline template of `ngIf` unless bound to a different value. + * - `else` template is blank unless its bound. * - * ### Example ([live demo](http://plnkr.co/edit/fe0kgemFBtmQOY31b4tw?p=preview)): + * # Most common usage + * + * The most common usage of the `ngIf` is to conditionally show the inline template as seen in this + * example: + * {@example common/ngIf/ts/module.ts region='NgIfSimple'} + * + * # Showing an alternative template using `else` + * + * 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 `