![](/img/trans.png)
[英]Angular2: How can a child component consistently listen to an input from the parent component after onInit
[英]How can we push each changes in input model class to parent component from child in Angular 13
我們如何將輸入模型類中的每個更改從 Angular 13 中的子組件推送到父組件
export class ChildComponent implements OnInit {
@Input() mdlInData: any;
@Output() mdlOutData = new EventEmitter<any>();
ngOnInit(): void {
this.mdlInData.subscribe((data) => {
this.mdlOutData.emit(this.mdlInData);
});
// This values Changes in HTML also -> TWO-WAY binding
this.mdlInData.id = 1;
this.mdlInData.Name = "Test";
this.mdlInData.phone = ['7867878', '768689678'];
//... More Properties
}
您需要實現OnChanges
生命周期掛鈎:
export class ChildComponent implements OnChanges {
@Input() input: any;
@Output() output = new EventEmitter<any>();
ngOnChanges(changes: SimpleChanges): void {
// if input property received new value from parent, emit back its value
if (changes?.input?.currentValue) {
this.output.emit(changes.input.currentValue);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.