Warm tip: This article is reproduced from stackoverflow.com, please click
angular angular-reactive-forms javascript rxjs

Angular Reactuve Form Control valueChanges get value changed field name , prev value and current val

发布于 2020-04-09 23:01:10
 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);
  });

using this code I am able to get the previous value and current value. what I want is there any way to get which field is changed, prev value and current value.

like field changed name: prop1, prev value:"", current value: "next value"

Questioner
Dinesh Ganesan
Viewed
43
henrikrank 2020-02-01 19:00

Instead of subscribing to theFormGroup.valueChanges observable, you could subscribe to the FormControl.valueChanges observable. See https://angular.io/api/forms/FormControl for the implementation. This way you could have access to the control itself. Maybe something like this?

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);
        });
    });