When I try to pass ASYNC data from parent to child component, I'm getting undefined message.
Because of ASYNC data, I suppose data coming from parent is not yet bound at OnInit.
In parent.component.html :
<my-child [childdata]="parentdata"></my-child>
In parent.component.ts :
interface SpotBB {
id: number;
name: string;
}
...
export class ParentComponent implements OnInit {
parentdata: Observable<SpotBB[]>;
...
ngOnInit() {
this.parentdata = this.spotsservice.getSpots();
// Call a service - Data stored in Firestore
}
In child.component.html :
<button (click)="mycheck()">TEST</button>
<div *ngFor="let spot of childdata | async" >
{{ spot.id }} --- {{ spot.name }} <!-- Works fine -->
</div>
In child.component.ts :
interface SpotBB {
id: number;
name: string;
}
...
export class ChildComponent implements OnInit {
@Input() childdata: Observable<SpotBB[]>;
copydata: Observable<SpotBB[]>;
...
mycheck() {
alert(JSON.stringify(this.copydata)); // --> !!! undefined !!!
}
ngOnInit() {
this.copydata = this.childdata; // COPY DATA NOT WORKING
}
console.log(this.childdate)
– Pardeep JainObservable
do not fetch data if you don't subscribe it. log thethis.childdata
on OnInit. Let me know it is undefined or not. – Ritwick Dey