AngularJS allow invalid values to be set on the model

The default behavior of AngularJS when you have a validator on an input field is to set the model to undefined if the validation failed.  This might be convenient for most cases, but sometimes you want to have the invalid value in your model.

It is really easy to tell Angular to allow invalid values on your model by specifying the option allowInvalid on the ng-model-options.

<input type="email" ng-model="model" ng-model-options="{allowInvalid: true}" />

I plunked something that demonstrates both the default behavior and the option enabled:

http://plnkr.co/edit/OlkS5U?p=info

Have fun
Christian

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.