p.
exported from angular2/annotations
defined in angular2/src/core/annotations/annotations.js (line 884)
:markdown
Directive that controls the instantiation, destruction, and positioning of inline template elements.
A viewport directive uses a ViewContainer
to instantiate, insert, move, and destroy views at runtime.
The ViewContainer
is created as a result of `` element, and represents a location in the current view
where these actions are performed.
Views are always created as children of the current View
, and as siblings of the `` element. Thus a
directive in a child view cannot inject the viewport directive that created it.
Since viewport directives are common in Angular, and using the full `` element syntax is wordy, Angular
also supports a shorthand notation: `` and `` are equivalent.
Thus,
```
```
Expands in use to:
```
```
Notice that although the shorthand places `*foo="bar"` within the `` element, the binding for the `Viewport`
controller is correctly instantiated on the `` element rather than the `` element.
## Example
Let's suppose we want to implement the `unless` behavior, to conditionally include a template.
Here is a simple viewport directive that triggers on an `unless` selector:
```
@Viewport({
selector: '[unless]',
properties: {
'unless': 'unless'
}
})
export class Unless {
viewContainer: ViewContainer;
prevCondition: boolean;
constructor(viewContainer: ViewContainer) {
this.viewContainer = viewContainer;
this.prevCondition = null;
}
set unless(newCondition) {
if (newCondition && (isBlank(this.prevCondition) || !this.prevCondition)) {
this.prevCondition = true;
this.viewContainer.clear();
} else if (!newCondition && (isBlank(this.prevCondition) || this.prevCondition)) {
this.prevCondition = false;
this.viewContainer.create();
}
}
}
```
We can then use this `unless` selector in a template:
```
```
Once the viewport instantiates the child view, the shorthand notation for the template expands and the result is:
```
```
Note also that although the `` template still exists inside the ``, the instantiated
view occurs on the second `` which is a sibling to the `` element.
.l-main-section
h2 Members
.l-sub-section
h3 constructor
pre.prettyprint
code.
constructor({
selector,
properties,
events,
hostListeners,
lifecycle
}:{
selector:string,
properties:any,
events:List,
lifecycle:List
}={})
:markdown