Angular 2: disable button if there are changes in form

Viewed 8237

I want to check if at least one field in my form is changed. If this scenario is true, the disableButton will be set to true, and false if there are no changes in the form. Below is my current code:

// this.hold is an array that temporary holds the previous values of 
// the form for comparison of the changes in form

if(this.data.process == 'update') {
   for(const f in form.value){
       if (form.value[f] == this.hold[f])
           this.disableButton = true;
       else
           this.disableButton = false;
   }
}

The problem is, the button only disables when ALL fields are changed. What should I add in my condition or in for loop?

4 Answers
editForm: FormGroup;
    this.editForm = this.fb.group({
      editCountry: ['', Validators.required],
      editDate: ['', Validators.required],
      editHolidayName: ['', Validators.required],
    });
     <div>
    <button [disabled]="!editForm.dirty" type="button" class="btn btn-primary miracle-submit m-t-5">Save</button>
    </div>
Related