I have two components: list and detail
In list component I want to render multiple detail components.
list.component.ts
@Component({
selector: 'detail',
templateUrl: './detail.component.html'
})
export class DetailComponent {
@ViewChild('details') details;
public items = {
// ...
}
}
list.component.html
<detail *ngFor="let item of items" #details></detail>
Notice the #details tempalte reference variable. I want to access all of the detail components. It is possible to make the #details variable an array ?
@ViewChildren('details') details:QueryList<DetailComponent>;
ngAfterViewInit() {
console.log(this.details.toArray());
this.details.changes.subscribe(changes => {
console.log(this.details.toArray());
});
}
See also angular 2 / typescript : get hold of an element in the template
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