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?
Asked
Active
Viewed 1.4k times
10
-
You already touched the input, then, It's dity! You can force Untouched by coding if the input is empty for example! – Fals Dec 07 '16 at 16:20
-
You should read the AbstractControl documentation which lists out what the states are and how they are set: https://angular.io/docs/ts/latest/api/forms/index/AbstractControl-class.html Generally, searching the documentation first before asking SO is a good idea. – silentsod Dec 07 '16 at 16:28
-
It is not clear that if the user changes a value and then changes it back to it's initial state, the value is still changed. The meaning of the word dirty is different. When someone makes something dirty and cleans it afterwards, it is not dirty any more! – Galdor Dec 12 '16 at 10:49
1 Answers
13
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...)

benny_boe
- 881
- 8
- 9
-
2
-
12Well eg: you've got a (clean and crystal clear) glass of water in front of you. You take the glass drink some the water and you refill it to its original ml count.. the ml of water in your glass might still be the same but it got your fingerprints on its.. hence it's dirty now.. ..so we've got our dishwasher called `markAsPristine()` for that – benny_boe Dec 12 '16 at 16:45
-
7Yes, `user3725805` is correct -- **that is touched**. Otherwise, why is `dirty` even there if after we rinse our glass and refill it our _dishwasher_ thinks there's still backwash??? I'm sorry, this makes absolute nonsense. So the Angular team said, "Hey, let's come up with a totally hideous definition of what 'dirty' means so that nobody can tell if the form looks **exactly** like it did before any input". **How can we tell if the form looks exactly as it did before input, then???????????????????** – Cody Sep 05 '17 at 17:17
-
2`touched` indicates that a user visited the control, regardless of whether the value has changed. This is useful as a separate event from dirty since we may want to know when the user has blurred (touched) an input, even if the value is the same (e.g. to show an invalid state only after they've visited the input). If you want to know whether or not a control has been changed, store the original value, listen for changes on the input with `valueChanges`, and compare the new value with the original. – Christian Jensen Oct 23 '19 at 17:21