gpt4 book ai didi

angular - 如何将 Angular @Input 值分配给另一个变量并填充到子变量中?

转载 作者:行者123 更新时间:2023-12-03 00:33:20 25 4
gpt4 key购买 nike

我有以下父级 html

<p>Parent</p>
<app-child [mainData]="mainData"></app-child>

父级.ts

mainData = [];
ngOnInit() {
this.myService((res)=>{
this.mainData = res;
})
}

子 html

<p>My JSON {{mainData | json}}</p> //Here getting result from parent

子.ts

@Input() mainData = [];
myDataCopy = [];
ngOnInit() {
this.myDataCopy = this.mainData;
console.log('My copy Data', this.myDataCopy); // Doesn't get result on here
}

如果我需要处理@Input数据怎么可能?

最佳答案

ngOnChanges生命周期 Hook 中尝试

子.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);
}
}

关于angular - 如何将 Angular @Input 值分配给另一个变量并填充到子变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58480827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com