Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 template reference variable with *ngFor

Tags:

angular

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 ?

like image 218
mspiderv Avatar asked Nov 26 '16 15:11

mspiderv


1 Answers

@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

like image 170
Günter Zöchbauer Avatar answered Jan 04 '23 15:01

Günter Zöchbauer