Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically set an Angular 2 form control to dirty?

How do I mark an Angular 2 Control as dirty in my code?

When I do it like this:

control.dirty = true; 

I get this error:

Cannot set property dirty of #<AbstractControl> which has only a getter 
like image 569
Eran Shabi Avatar asked Jun 15 '16 19:06

Eran Shabi


People also ask

How do you make angular forms dirty?

You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.

What is form control dirty?

To prevent the validator from displaying errors before the user has a chance to edit the form, you should check for either the dirty or touched states in a control. When the user changes the value in the watched field, the control is marked as "dirty"

What is clean and preferred way to reset all objects in angular form?

In a model-driven form to reset the form we just need to call the function reset() on our myform model. The form now resets, all the input fields go back to their initial state and any valid , touched or dirty properties are also reset to their starting values.


1 Answers

You should use the markAsDirty method, like this:

control.markAsDirty(); 

This will also mark all direct ancestors as dirty to maintain the model.

Docs link

like image 129
Eran Shabi Avatar answered Sep 22 '22 06:09

Eran Shabi