Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 635
Vladimir Humeniuk Avatar asked May 17 '18 08:05

Vladimir Humeniuk


People also ask

How do you know if reactive form value is changed?

You can use rxjs operators to check values.

How do you know if a form is dirty?

if you want to see if the form is dirty you should check the viewModel in kendo way sample. basically I've created a viewModel which is impements the ObservableObject interface and has a two way binding with the form's container.

Which class is applied on a form control if its value is changed?

ValueChanges of FormGroup The ValueChanges event of FormGroup or FormArray is fired, whenever the value of any of its child controls value changes.


1 Answers

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

like image 110
Pranay Rana Avatar answered Oct 15 '22 20:10

Pranay Rana