Is there something special going on with input type="email" and ng-model attribute? If the input is email, then the model doesnt update. If I change the input type to text, number or date it updates correctly.
Bug or some special magic email validation behavior that I dont understand?
The <input type="email"> defines a field for an e-mail address. The input value is automatically validated to ensure it is a properly formatted e-mail address. To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute.
ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form. We can use ngModel with: input.
NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.
ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.
It does some validation on then input, so you need to have entered a valid email address before it is bound to the model.
This is the regex that is used:
/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/
Basically you need to input an address that is at least [email protected]
It's not a bug, it's only update when we type correct email address format for email validation.
Add this attribute ng-model-options="{'allowInvalid': true}"
to allow invalid email input.
A complement, you can use properties on you form to see if your email is valid, like this :
HTML:
<form name="myForm" ng-submit="submit()">
<input type="email" ng-model="email1" name="email1" />
</form>
Javascript:
//[formName].[inputFieldName].property
myForm.email1.$pristine;
// Boolean. True if the user has not yet modified the form.
myForm.email1.$dirty
// Boolean. True if the user has already modified the form.
myForm.email1.$valid
// Boolean.True if the the form passes the validation.
myForm.email1.$invalid
// Boolean. True if the the form doesn't pass the validation.
myForm.email1.$error
Reference
Starting from Angular 1.3, you can easily overwrite the 'email' validator and make it always return true.
angular
.module('myApp', [])
.controller('MainController', function() {
this.email = '';
})
.directive('noEmailValidation', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elm, attr, ctrl) {
ctrl.$validators['email'] = function() {
return true;
};
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<div ng-app="myApp">
<form ng-controller="MainController as main">
<div>Email: {{main.email}}</div>
<input type="email" ng-model="main.email" no-email-validation>
</form>
</div>
Enjoy.
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