First, I need to send some json data from parent to child component, but I load the data from http request in ngOnInit event and when I submit the data in normal way, the data is not sent to my child component, here is my first try :
data: TransSalesOrder = new TransSalesOrder();
ngOnInit(): void {
if (this.dataId > 0) {
const data: SelectItem = new SelectItem();
data.id = this.dataId;
const sb = this.transSalesOrderService.view(data).subscribe(response => {
this.transSalesOrderForm.patchValue(response);
this.data = response;
if (this.transSalesOrderForm.get('isDeleted').value) {
this.editButton = false;
this.deleteButton = false;
this.isDeleted = true;
}
}, err => {
this.openSnackBar(err);
});
this.subscriptions.push(sb);
}
}
Then in my parent html, I sent the data like this :
<app-trans-sales-order-detail-list [salesOrderDetailsInput]="data.transSalesOrderDetail"></app-trans-sales-order-detail-list>
Then when I console.log in the child component.ts :
@Input() salesOrderDetailsInput: any;
constructor() { }
ngOnInit(): void {
console.log(this.salesOrderDetailsInput);
}
It's print out 'undefined'
Then I think the parent is too late to load the data, and here is my second try, I need ngOnChanges event :
ngOnChanges(changes: SimpleChanges) {
console.log(changes.salesOrderDetailsInput.currentValue);
}
Then I can get my data that sent from parent, but I read somewhere in stackoverflow, some said there is async function needed to add from the parent value and the @Input in child component should retrieve as Observable, then I assume if I can do this, I will no longer need ngOnChanges, here is how I try :
In my parent code :
<app-trans-sales-order-detail-list [salesOrderDetailsInput]="data.transSalesOrderDetail | async"></app-trans-sales-order-detail-list>
But it give me error ERROR Error: InvalidPipeArgument: '' for pipe 'AsyncPipe'
when compile and error Argument type TransSalesOrderDetail[] is not assignable to parameter type Observable<unknown> | Promise<unknown>
in my Intellij Idea ide
Here is my child component.ts :
@Input() salesOrderDetailsInput: Observable<any>;
What did I miss here?