[英]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.