QueryList not updated with dynamic component

I came across the same issue and found that you can subscribe to the query list changes which will fire if you add or remove an element. Hopefully will help someone else down the line...

@ViewChildren(MyComponent) myComp: QueryList<MyComponent>;

ngAfterViewInit() {
    this.myComp.changes
            .subscribe((queryChanges) => {
                // Do something
            });
}

Got a confirmation from the Angular chat forum that @ViewChildren/@ViewChild can only read elements which are already available in the view. Since, dynamic components are not available during view init, I had to maintain a list of all such dynamically generated components to keep track of them.


After all your child components ngAfterViewInit method will execute

export class ParentComponent implements OnInit, AfterViewInit {

 ngAfterViewInit(){

     ///////////////// your operations
 }

So you cannot perform operations in ngOnInit

Tags:

Angular