NVO NVO - 3 months ago 56
AngularJS Question

Angular: check value ng-blur

I'm trying to build a form in Angular, but i'm new with it, so I need some help :)

I have a field 'email' which should be checked when the user leave that field. A function in my controller should to check the entered value if it already exist in the database (ajax/http).

I found the 'ng-blur' function and have created a function in my controller to check the email. But, the $scope.user.email is undefined, and I don't known what i'm doing wrong. Can somebody help me?

This is my code so far:

HTML (jade)

p.claim-text Email
abbr.required.claim-text(title='required') *
input#billing_email.input-text.form-control.claim-input(type='email', placeholder='Email', value='', name='email', ng-model='user.email', ng-blur='checkEmail()' required)
p(ng-show="registerForm.email.$invalid && !registerForm.email.$pristine").help-block Invalid emailadres
p(ng-show="emailExists").help-block User exists! Click here to login


Controller function

$scope.checkEmail = function(){
console.log($scope.user.email);
$http({
method: 'GET',
url: '[someurl]',
data: $scope.user.email,
}).then(function successCallback(response){
//if response is true, set emailExists to true;
}, function errorCallback(response){

});

}

Answer

You can pass the email to checkEmail function

input#billing_email.input-text.form-control.claim-input(type='email', placeholder='Email', value='', name='email', ng-model='user.email', ng-blur='checkEmail()' required)

Then in controller you just need to get the email from argument

$scope.checkEmail = function(){
        console.log($scope.user.email);
        $http({
            method: 'GET',
            url: '[someurl]',
            data: $scope.user.email,
        }).then(function successCallback(response){
            //if response is true, set emailExists to true;
        }, function errorCallback(response){

        });

    }