Reputation:
I am implementing the structural directive in Angular2.
In the docs, I saw that I need to inject the TemplateRef
to get the template element and the ViewContainerRef
.
class TestDirective {
constructor( private templateRef : TemplateRef<any>, private viewContainer : ViewContainerRef ) {
// this.viewContainer.createEmbeddedView(this.templateRef);
}
}
<template [ngIf]="condition">
<p>
Our heroes are true!
</p>
</template>
In this case what I don't understand which element is the ViewContainerRef
?
Upvotes: 4
Views: 2318
Reputation: 657731
If you inject ViewContainerRef
the element is the host element of TestDirective
. If you use @ViewChild(templateVarNameOrType)
or @ContentChild(templateVarNameOrType)
it's the element where templateVarNameOrType
matches.
You should be aware that this.viewContainer.createEmbeddedView()
or this.viewContainer.createComponent()
creates the element or component as sibling of the element ViewContainerRef
points to, not as many expect as child.
Upvotes: 2