2015-05-20 17:19:46 -07:00
|
|
|
import {Directive} from 'angular2/annotations';
|
|
|
|
import {ViewContainerRef, ProtoViewRef} from 'angular2/core';
|
2015-02-05 13:08:05 -08:00
|
|
|
import {isBlank} from 'angular2/src/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
|
|
|
*
|
|
|
|
* # Example:
|
|
|
|
*
|
|
|
|
* ```
|
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>
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* # Syntax
|
|
|
|
*
|
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-04-06 11:47:38 +02:00
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/directives
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-05-26 15:54:10 +02:00
|
|
|
@Directive({selector: '[ng-if]', properties: ['ngIf']})
|
2015-05-11 15:58:59 -07:00
|
|
|
export class NgIf {
|
2015-04-27 09:26:55 -07:00
|
|
|
viewContainer: ViewContainerRef;
|
2015-04-29 15:07:55 -07:00
|
|
|
protoViewRef: ProtoViewRef;
|
2014-12-17 10:01:08 +01:00
|
|
|
prevCondition: boolean;
|
|
|
|
|
2015-05-20 17:19:46 -07:00
|
|
|
constructor(viewContainer: ViewContainerRef, protoViewRef: ProtoViewRef) {
|
2015-02-12 11:54:22 +01:00
|
|
|
this.viewContainer = viewContainer;
|
2014-12-17 10:01:08 +01:00
|
|
|
this.prevCondition = null;
|
2015-04-29 15:07:55 -07:00
|
|
|
this.protoViewRef = protoViewRef;
|
2014-12-17 10:01:08 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:58:59 -07:00
|
|
|
set ngIf(newCondition /* boolean */) {
|
2014-12-17 10:01:08 +01:00
|
|
|
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
|
|
|
|
this.prevCondition = true;
|
2015-04-29 15:07:55 -07:00
|
|
|
this.viewContainer.create(this.protoViewRef);
|
2014-12-17 10:01:08 +01:00
|
|
|
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
|
|
|
|
this.prevCondition = false;
|
2015-02-12 11:54:22 +01:00
|
|
|
this.viewContainer.clear();
|
2014-12-17 10:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|