0

I'm using $asyncValidators to validate a field but would like it to trigger only when the field loses focus and not on every change. Is this possible or must I use something else than $asyncValidators? Code example of using $asyncValidators (taken from the documentation)

ngModel.$asyncValidators.uniqueUsername = function (modelValue, viewValue) {
    var value = modelValue || viewValue;

    // Lookup user by username
    return $http.get('/api/users/' + value).
        then(function resolved() {
            //username exists, this means validation fails
            return $q.reject('exists');
        }, function rejected() {
            //username does not exist, therefore this validation passes
            return true;
        });
};

1 Answer 1

1

I think you can use the ngModelOptions directive to achieve that.

ng-model-options="{ updateOn: 'blur' }"

You can force your model to be "evaluated" only on some predefined events. In your case, you want to evaluate it when you leave the field, so on the blur event.

Official documentation of ngModelOptions directive

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it works! Maybe I should read the documentation a bit closer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.