this.form = this.fb.group({
prop1: '',
prop2: ''
});
this.form.valueChanges.pipe(startWith(null), pairwise())
.subscribe(([prev, next]: [any, any]) => {
console.log('PREV2', prev);
console.log('NEXT2', next);
});
使用此代码,我可以获取之前的值和当前值。我想要的是有什么方法可以获取更改哪个字段,上一个值和当前值。
如字段更改名称:prop1,上一个值:“”,当前值:“下一个值”
FormGroup.valueChanges
您可以订阅可观察对象,而不是订阅可FormControl.valueChanges
观察对象。有关实现,请参见https://angular.io/api/forms/FormControl。这样,您就可以访问控件本身。也许是这样的吗?
Object.keys(this.form.controls).forEach(key => {
this.form.controls[key].valueChanges.pipe(startWith(null), pairwise())
.subscribe(([prev, next]: [any, any]) => {
console.log(key) // your FormControl Identifier
console.log('PREV2', prev);
console.log('NEXT2', next);
});
});