8

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 ?

Kim Kern
  • 54,283
  • 17
  • 197
  • 195
mspiderv
  • 509
  • 7
  • 15

1 Answers1

7
@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

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567