How to get access to child package object in Angular

I used a package in app.component.html

    <app-my-test2 *ngIf="data" #MyTest2Component></app-my-test2>

here's app-my-test2 package. (with ngPackager)

and used in app.component.ts

@ViewChild('MyTest2Component', {static: true}) myTest2Component: MyTest2Component;

befor, convert app-my-test2 to package, all work correctly and i access app-my-test2 "this", but after convert app-my-test2 to package, "this" returned undefind

1 answer

  • answered 2020-10-28 05:48 M.H.A

    The problem was my *ngIf. The *ngIf directive was killing my controls component so I couldn't reference it.

    The issue as previously mentioned is the ngIf which is causing the view to be undefined. The answer is to use ViewChildren instead of ViewChild. I had similar issue where I didn't want a grid to be shown until all the reference data had been loaded.

     @ViewChildren("MyTest2Component") public myTest2Component: QueryList<MyTest2Component>
    
    
    public ngAfterViewInit() {
    
        this.myTest2Component.changes.subscribe((comps: QueryList <MyTest2Component>) =>
        {
            console.log(comps.first) ;
        });
    
    }
    

    document