Whenever you use ng-model on an input or select tag, angular internally manages two values for the field, one is $viewValue
and other is $modelValue
$viewValue -> Used for display purpose on view
$modelValue-> Actual value which is used inside scope.
When using an input tag with type="email"
Angular constantly validates the input value.
And if the value does not validate as a correct email, angular internally will set $modelValue
to undefined
and will set the form.fieldName.$error.fieldName attribute to true. So that field becomes invalid
.
If you check the value of form.fieldName.$modelValue inside the controller you will find it as undefined
.
So setting the model to ‘undefined’ in the controller, when the field is already invalid, changes nothing.
But if you set it to null
or ""
it will work as $modelValue
and $viewValue
both get changed – making the field valid again.
Hope this has cleared your understanding. Thanks.