2015-02-12 11:54:22 +01:00
|
|
|
import {Viewport} from 'angular2/src/core/annotations/annotations';
|
|
|
|
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
|
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}.
|
|
|
|
*
|
|
|
|
* If the expression assigned to `if` evaluates to a false value then the element is removed from the
|
2015-04-06 11:47:38 +02:00
|
|
|
* DOM, otherwise a clone of the element is reinserted into the DOM.
|
|
|
|
*
|
|
|
|
* # Example:
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* <div *if="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 *if="condition">...</div>`
|
|
|
|
* - `<div template="if condition">...</div>`
|
|
|
|
* - `<template [if]="condition"><div>...</div></template>`
|
|
|
|
*
|
2015-04-10 12:45:02 +02:00
|
|
|
* @exportedAs angular2/directives
|
2015-03-31 22:47:11 +00:00
|
|
|
*/
|
2015-02-12 11:54:22 +01:00
|
|
|
@Viewport({
|
2015-02-04 22:27:31 +01:00
|
|
|
selector: '[if]',
|
2015-04-09 21:20:11 +02:00
|
|
|
properties: {
|
2015-02-17 21:55:18 +01:00
|
|
|
'condition': 'if'
|
2014-12-17 10:01:08 +01:00
|
|
|
}
|
|
|
|
})
|
2015-02-04 22:27:31 +01:00
|
|
|
export class If {
|
2015-02-12 11:54:22 +01:00
|
|
|
viewContainer: ViewContainer;
|
2014-12-17 10:01:08 +01:00
|
|
|
prevCondition: boolean;
|
|
|
|
|
2015-02-12 11:54:22 +01:00
|
|
|
constructor(viewContainer: ViewContainer) {
|
|
|
|
this.viewContainer = viewContainer;
|
2014-12-17 10:01:08 +01:00
|
|
|
this.prevCondition = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
set condition(newCondition) {
|
|
|
|
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
|
|
|
|
this.prevCondition = true;
|
2015-02-12 11:54:22 +01:00
|
|
|
this.viewContainer.create();
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|