When you change the field of NgModel, it automatically change model.prisitne to true.
When you submit the form, it does't change the "pristine", no question, this is not a bug.
But in my case, I show errors when "pristine" is true and when I submit the form, I need to show validation errors and I think when you submit the form, we can say that the fields in this form touched, because you can't submit the invalid form. But in Angular2 it works in different way.
So, any way to say that the form controls/fields is touched (pristine = true) in code/component?
let email:AbstractControl = this.frm.form.controls['email'];
Set email "prisitne" true.
Using $setPristine will set the $pristine property of the form to true and $dirty to false. $setDirty will do the contrary.
A control is pristine if the user has not yet changed the value in the UI. A control is dirty if the user has changed the value in the UI. True if the control is marked as touched .
This directive is used by itself or as part of a larger form. Use the ngModel selector to activate it. It accepts a domain model as an optional Input . If you have a one-way binding to ngModel with [] syntax, changing the domain model's value in the component class sets the value in the view.
email.markAsPristine();
email.markAsTouched();
email.reset();
or
this.frm.reset();
See also https://angular.io/docs/ts/latest/api/forms/index/AbstractControl-class.html
You can use this shorter method to get a control
let email:AbstractControl = this.frm.get('email']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With