Detect if data in an Angular form (not reactive) was changed

Vladimir Humeniuk picture Vladimir Humeniuk · May 17, 2018 · Viewed 23.6k times · Source

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?

Answer

Pranay Rana picture Pranay Rana · May 17, 2018

do this , check for dirty flag, which tells form dirty or not

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

form becomes dirty if you change some value in it.

Check here for detail : https://angular.io/guide/forms enter image description here