UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

55 lines (54 loc) 2.47 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Directive, ViewContainerRef, TemplateRef } from 'angular2/core'; import { isBlank } from 'angular2/src/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)): * * ``` * <div *ngIf="errorCount > 0" class="error"> * <!-- Error message displayed when the errorCount property on the current context is greater * than 0. --> * {{errorCount}} errors detected * </div> * ``` * * ### Syntax * * - `<div *ngIf="condition">...</div>` * - `<div template="ngIf condition">...</div>` * - `<template [ngIf]="condition"><div>...</div></template>` */ export let NgIf = class NgIf { constructor(_viewContainer, _templateRef) { this._viewContainer = _viewContainer; this._templateRef = _templateRef; this._prevCondition = null; } set ngIf(newCondition /* 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(); } } }; NgIf = __decorate([ Directive({ selector: '[ngIf]', inputs: ['ngIf'] }), __metadata('design:paramtypes', [ViewContainerRef, TemplateRef]) ], NgIf);