I have a list in the child component. I want to access this list from the parent component (AppComponent
) and display it using @ViewChild
decorator, but the list is always undefined in the parent component
export class CategorieComponent implements OnInit {
@Output() listCategories: Categorie[];
constructor(private http: HttpClient) {
this.http.get<Categorie[]>('api/Categorie').subscribe(value => {
this.listCategories = value;
});
}
ngOnInit() {
}
}
app-component.ts
@ViewChild(CategorieComponent, { static: false })
private categoryComponent: CategorieComponent;
.
.
.
ngAfterViewInit() {
this.listCategories = this.categoryComponent.listCategories;
}
app.component.html
<option *ngFor="let cat of listCategories" [value]="cat.idCat" [selected]="cat.idCat == projet.idCat">
{{cat.nomCat}}
</option>