gpt4 book ai didi

Angular - FormGroup 上的 ngOnChange

转载 作者:行者123 更新时间:2023-12-01 22:10:04 28 4
gpt4 key购买 nike

我需要检测 FormGroup 字段的实时变化。我有一个简单的父子组件结构如下:

父组件 View

<my-child [myForm]="myForm"></my-child>

子组件 Controller

@Input()
myForm: FormGroup;

ngOnChanges(changes: SimpleChanges) {
console.log('changes', changes)
}

问题是当表单字段发生变化时没有检测到任何变化。有没有办法触发 ngOnChanges 任何形式的变化?谢谢。

最佳答案

您可以使用以下内容:

this.myForm.valueChanges.subscribe(changes => {
// do what you need with the form fields here
// you can access a form field via changes.fieldName
});

关于Angular - FormGroup 上的 ngOnChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48553120/

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