I ran into a problem when I want to check whether the mail has been registered or not in the db, but only on the blur event ( I dont want it to check on every change ). So I wrote a directive that I can re-use in other places in my app. But the problem is the directive keeps checking even when I still not blur out, Here is my plnkr for reference: http://plnkr.co/edit/eUPFxIc78Wkl4mCX6hrk?p=preview
And here is my directive code:
app.directive('checkEmail', function(userService){
return{
restrict: "A",
require:'ngModel',
link: function( scope, ele, attrs, ctrl ){
ele.bind('blur', function(){
console.log("Run in blur!");
ctrl.$parsers.unshift(function( email ){
console.log("Email is ", email);
// Checking to see if the email has been already registered
if( ele.val() && userService.isDuplicateEmail(email) ){
ctrl.$setValidity('isDuplicatedEmail', true );
}else{
ctrl.$setValidity('isDuplicatedEmail', false );
}
});
})
}
}
})
Sorry I'm new to angular and this simple task already drives me nuts. PLease take a look at my directive and tell me what I can do to fix this problem. Thanks in advance