I just met strange behavior from Angular:
Here's the script:
In the registration form, I want to check the uniqueness of email (via an HTTP call to the server).
So I created a directive called emailUnique , whose client code is:
<form name="form" novalidate> <input name="email" type="email" ng-model="user.email" required email-unique/> </form>
For the rest of the message, suppose the user types: michael , i.e., obviously invalid mail.
Let's look at the interesting part of my directory code, invoking the behavior that interests me:
angular.module('directives.emailUnique', []) .directive('emailUnique', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, el, attrs, ctrl) { ctrl.$parsers.push(function (viewValue) { console.log(viewValue);
Before giving an answer, at first glance, it will logically respond:
undefined
Why? Because:
- The exact attribute is
type="email" , not just type="text" michael not a valid mail.- Angular The compiler is supposed to conform to the classic HTML behavior.
After testing, the answer will be undefined , as expected. My complete directory logic will be based on this, and everything works fine.
Now rename the directive: emailUnique becomes somethingUnique .
Customer now:
<input name="email" type="email" ng-model="user.email" required something-unique/>
Surprise: console.log(viewValue) : michael now displayed, not undefined ...
It is clear that starting with email for a name has a strange effect when working with the email field in this case.
My question is simple: Is there a good reason? A possible mistake? Perhaps I misunderstood some concept?
Some additional notes:
- The Angular email field documentation with angular does not specify the
email attribute, which may interfere with email-unique . Indeed, it is based on type="email" - I ran into the same problem whether the
novalidate form attribute is novalidate or not.