Do you have any idea about the strange problem below?
I am passing data from a parent to a child component that is returned from a service method returning data as Observable<DemoModel>
. But, when child component is loading, the data is undefined and it is only filled after ngAfterViewInit
(I also tried getting the data on this method, but the data is still undefined). So, I also tried to apply some ngOnchanges
approach, but the problem is much more related to that the data retrieved from Parent Component is not ready while Child Component is loading (I also tried to use async, etc. instead of subscribe. How should I get data to make it ready while child component is loading?
Parent and Child Components are shown below:
Parent Comp
<child-component
[courses]="courses|async"
>
</child-component>
courses: any;
this.service.listCourses().subscribe((course: Course) => {
this.courses = course;
});
Child Comp
private courses: any;
@Input()
set data(data: any) {
this.courses.next(data);
}
myControl = new FormControl('');
ngAfterViewInit() {
// >>> THIS THROWS ERROR AS this.courses is undefined
this.myControl.setValidators([
Validators.required,
forbiddenNamesValidator(this.courses)
]);
}
I also tried to use some *ngIf
in html, but as the this.courses
parameter is used in the methods, it does not make any sense to check the data in html.
The problem may be caused by subscribe method, but I also tried to use promise (I am not sure if I used it properly).