Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Reactive forms : how to get just changed values

Im building a reactive forms using angular 6, this form contain 3 attributes (name,age,phone) and i would to get just the changed values not all form values.

this.refClientForm = this.formBuilder.group({
  name: [],
  phone: [],
  age: []
});

for the form listener :

 this.refClientForm.valueChanges.subscribe(values => console.log(values))

but i got always all form values.

like image 416
AHméd Net Avatar asked Dec 04 '18 13:12

AHméd Net


People also ask

How do you know if Reactive form value is changed?

You can use rxjs operators to check values.


1 Answers

You can check all controls for dirty-flag. See https://angular.io/api/forms/FormControl

getDirtyValues(form: any) {
        let dirtyValues = {};

        Object.keys(form.controls)
            .forEach(key => {
                let currentControl = form.controls[key];

                if (currentControl.dirty) {
                    if (currentControl.controls)
                        dirtyValues[key] = this.getDirtyValues(currentControl);
                    else
                        dirtyValues[key] = currentControl.value;
                }
            });

        return dirtyValues;
}
like image 58
J. Knabenschuh Avatar answered Oct 29 '22 14:10

J. Knabenschuh