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
  • 43,715
  • 16
  • 163
  • 170
mspiderv
  • 412
  • 6
  • 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
  • 558,509
  • 191
  • 1,911
  • 1,506