formsangular

angular2 formcontrol stays dirty even if set to original value


When I change a value in an input field, the corresponding form-control is set to dirty. When I revert my change (by typing in the input field) the form-control stays dirty. Is this intended, is this a bug or do I do something wrong?


Solution

  • Well, yes it's intended to work that way since:

    A control is dirty if the user has changed the value in the UI. (docs entry)

    ..and not if the value is different from the starting value.


    In case you want to revert the dirty state you could use the markAsPristine() (docs entry) on your AbstractControl. (eg trigger it by click on a button or when subscribing to the valueChanges observable of the AbstractControl and compare the new value to a previously stored one...)