Issue
I have following parent html
<p>Parent</p>
<app-child [mainData]="mainData"></app-child>
parent.ts
mainData = [];
ngOnInit() {
this.myService((res)=>{
this.mainData = res;
})
}
Child html
<p>My JSON {{mainData | json}}</p> //Here getting result from parent
child.ts
@Input() mainData = [];
myDataCopy = [];
ngOnInit() {
this.myDataCopy = this.mainData;
console.log('My copy Data', this.myDataCopy); // Doesn't get result on here
}
If I need to process the @Input data how it possible?
Solution
Try it in ngOnChanges lifecycle hook
child.ts
ngOnChanges(changes: SimpleChanges) {
const mainDataChange = changes.mainData ;
if (mainDataChange) {
this.myDataCopy = this.mainData;
// or, this.myDataCopy = mainDataChange.currentValue;
console.log('My copy Data', this.myDataCopy);
}
}
Answered By - Adrita Sharma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.