import {Directive, ViewContainerRef, TemplateRef} from '@angular/core'; import {isBlank} from '../facade/lang'; /** * Removes or recreates a portion of the DOM tree based on an {expression}. * * If the expression assigned to `ngIf` evaluates to a false value then the element * is removed from the DOM, otherwise a clone of the element is reinserted into the DOM. * * ### Example ([live demo](http://plnkr.co/edit/fe0kgemFBtmQOY31b4tw?p=preview)): * * ``` *
* * {{errorCount}} errors detected *
* ``` * * ### Syntax * * - `
...
` * - `
...
` * - `` * * @stable */ @Directive({selector: '[ngIf]', inputs: ['ngIf']}) export class NgIf { private _prevCondition: boolean = null; constructor(private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef) { } set ngIf(newCondition: any /* boolean */) { if (newCondition && (isBlank(this._prevCondition) || !this._prevCondition)) { this._prevCondition = true; this._viewContainer.createEmbeddedView(this._templateRef); } else if (!newCondition && (isBlank(this._prevCondition) || this._prevCondition)) { this._prevCondition = false; this._viewContainer.clear(); } } }