I have a angular2 component for which the template look like this:
<div>
<div><ng-template #left></ng-template></div>
<div><ng-template #right></ng-template></div>
</div>
I would like to be able to retrieve a reference to all ng-template
element using ViewChildren
but I have no idea the type I need to pass between bracket.
Descriptionlink. Use to get the QueryList of elements or directives from the view DOM. Any time a child element is added, removed, or moved, the query list will be updated, and the changes observable of the query list will emit a new value. View queries are set before the ngAfterViewInit callback is called.
pTemplate is a custom-made directive by PrimeNG. Its purpose is to link a template defined by you (the user) to a container defined by the library (PrimeNG). That enables the user to provide a custom template that is displayed inside a component made by the library.
ViewChild is used to select an element from component's template while ContentChild is used to select projected content.
Working with @ViewChildren is similar to @ViewChild, but the difference between the two is @ViewChildren provides a list of element references rather than returning a single reference. It is used to reference multiple elements. We can then iterate the list of the element referenced by the variable.
@ViewChildren(TemplateRef) templates: QueryList<TemplateRef>;
ngAfterViewInit() {
console.log(this.templates.toArray());
}
or
@ViewChild('left') left:TemplateRef;
@ViewChild('right') right:TemplateRef;
ngAfterViewInit() {
console.log(this.left, this.right);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With