26

I have Angular form (not reactive), with data binded in ngModel:

 <form #form="ngForm">
  <input [(ngModel)]="user.name">
  <input [(ngModel)]="user.color">

  <button type="submit">Save</button>
 </form>

How can i disable submit button if binded data has not been changed?

Michael Dimmitt
  • 826
  • 10
  • 23
Volodymyr Humeniuk
  • 3,411
  • 9
  • 35
  • 70

4 Answers4

39

You can check the dirty flag, which tells you if the form is dirty or not.

<button type="submit" [disabled]="!form.dirty">Save</button>

The form becomes dirty if you change some value in it.

Check here for more details: https://angular.io/guide/forms

enter image description here

Tim
  • 5,435
  • 7
  • 42
  • 62
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • 12
    But what if i erase 1 symbol in input and then wright it again (the value is the same, but form was changed)? – Volodymyr Humeniuk May 17 '18 at 09:11
  • 3
    yes if you change value it becomes dirty, if you want to do like that then you need to write code , because there is no direct way for that – Pranay Rana May 17 '18 at 09:13
4

According to your comment 'But what if i erase 1 symbol in input and then wright it again (the value is the same, but form was changed)?' I suggest this solution.

The general idea is to store the initial value of the form as separate object (just cloning it). And then create a boolean function which simply iterate through the key-values and compare Updated data with the Initial data. After this just bind the result of this function to your submit button [disabled]="yourCheckMethod(form.value)".

shohrukh
  • 2,989
  • 3
  • 23
  • 38
1

You can try it with the pristine property like this:

<button type="submit" [disabled]="form.pristine">Save</button>

This property checks if your form has changed since it was loaded.

ochs.tobi
  • 3,214
  • 7
  • 31
  • 52
1

I had a situation where I did not have a form, adapting it to the question asked here, although mine handles on click and not disabling the button. Angular 7 with TypeScript:

    <!-- user.component.html -->
    .....
    .....
    <div>
      <input [(ngModel)]="user.name">
      <input [(ngModel)]="user.color">
      <button (click)="save()">Save</button>
    </div>
  
    // user.component.ts
    .....
    .....
    lastObjectHash: string;
    User: user = { name: "joe", color: "blue"};  // with name and color on type User 
    
    // Not really a hash, but let's call it that
    getObjectHash(): Promise<string> {
      return util.encodeBase64(JSON.stringify(this.user));
    }

    ngAfterViewInit(): void {
      this.getObjectHash().then(value => this.lastObjectHash = value);
    }

    save() {
      this.getObjectHash().then(value => { 
        if (this.lastObjectHash === value) {
          alert("You did not change name or color");
          return;
        }
        // Save user changes....
        this.userService.save(this.user);  // Or whatever...
      }); 
    }

    // util.ts
    // Just a utility function to BASE64 encode
    .....
    .....
    export const encodeBase64 = async (textString) => {
      return btoa(textString);
    };
leoncc
  • 233
  • 3
  • 6