简体   繁体   English

使用响应式表单侦听表单中的更改值

[英]Listen for changes values in a form using Reactive Forms

Is there a way to listen for changes (only) in FormGroup With changes, I mean only the exact key and value that changed?有没有办法侦听 FormGroup 中的更改(仅)随着更改,我的意思是仅更改了确切的键和值?

For example if the input name changes, something like {name: //newValue} should be emmitet例如,如果输入name改变,像{name: //newValue}这样的东西应该是 emmitet

A Solution I don't prefer is to subscribe to valueChanges of every single FormControl.我不喜欢的解决方案是订阅每个 FormControl 的 valueChanges。

I provided a little example for testing here .在这里提供了一个测试的小例子。

您可以在FormGroup上订阅valueChanges ,但是您必须检查各个值以了解哪个FormControl的值已更改。

you can use pairwise你可以成对使用

  .pipe(
   startWith({}),
   pairwise()
    ,tap(([previousValue, currentValue]) => {
    /** compare values here */
})
)
;

or或者

 .pipe(
   startWith({}),
   pairwise()
)
.subscribe(([previousValue, currentValue]) => {
    /** compare values here */
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM