2015-09-03 22:01:36 -07:00
|
|
|
import {Directive} from 'angular2/src/core/metadata';
|
2015-10-02 07:37:23 -07:00
|
|
|
import {ViewContainerRef, TemplateRef} from 'angular2/src/core/linker';
|
2015-08-20 14:28:25 -07:00
|
|
|
import {isBlank} from 'angular2/src/core/facade/lang';
|
2014-12-17 10:01:08 +01:00
|
|
|
|
2015-03-31 22:47:11 +00:00
|
|
|
/**
|
2015-04-10 11:15:01 -07:00
|
|
|
* Removes or recreates a portion of the DOM tree based on an {expression}.
|
|
|
|
*
|
2015-06-02 08:55:58 -07:00
|
|
|
* If the expression assigned to `ng-if` evaluates to a false value then the element
|
|
|
|
* is removed from the DOM, otherwise a clone of the element is reinserted into the DOM.
|
2015-04-06 11:47:38 +02:00
|
|
|
*
|
2015-09-17 15:28:11 -07:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/fe0kgemFBtmQOY31b4tw?p=preview)):
|
2015-04-06 11:47:38 +02:00
|
|
|
*
|
|
|
|
* ```
|
2015-05-11 15:58:59 -07:00
|
|
|
* <div *ng-if="errorCount > 0" class="error">
|
2015-05-20 17:19:46 -07:00
|
|
|
* <!-- Error message displayed when the errorCount property on the current context is greater
|
|
|
|
* than 0. -->
|
2015-04-06 11:47:38 +02:00
|
|
|
* {{errorCount}} errors detected
|
|
|
|
* </div>
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-19 15:37:32 +01:00
|
|
|
*##Syntax
|
2015-04-06 11:47:38 +02:00
|
|
|
*
|
2015-05-11 15:58:59 -07:00
|
|
|
* - `<div *ng-if="condition">...</div>`
|
|
|
|
* - `<div template="ng-if condition">...</div>`
|
|
|
|
* - `<template [ng-if]="condition"><div>...</div></template>`
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-09-30 20:59:23 -07:00
|
|
|
@Directive({selector: '[ng-if]', inputs: ['ngIf']})
|
2015-05-11 15:58:59 -07:00
|
|
|
export class NgIf {
|
2015-08-13 13:18:41 -07:00
|
|
|
private _prevCondition: boolean = null;
|
2014-12-17 10:01:08 +01:00
|
|
|
|
2015-08-13 13:18:41 -07:00
|
|
|
constructor(private _viewContainer: ViewContainerRef, private _templateRef: TemplateRef) {}
|
2014-12-17 10:01:08 +01:00
|
|
|
|
2015-05-11 15:58:59 -07:00
|
|
|
set ngIf(newCondition /* boolean */) {
|
2015-08-13 13:18:41 -07:00
|
|
|
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();
|
2014-12-17 10:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|