Angular js $asyncValidator "Undefined is not a function!" when passing promises

258 Views Asked by At

Just trying to refactor my code and intend to have the following structure:

Services:

} }]).factory('UsernameRESTService', ['Restangular',function(Restangular){
    var restAngular = 
        Restangular.withConfig(function(Configurer) {
            Configurer.setBaseUrl('http://localhost:2000/');
        });

        return {
            getCarrier: function(carrierCode) {
                var usernamePromise=restAngular.one(carrierCode).get();
                return usernamePromise;
            }
}}]).factory('UsernameValidationHelper',['UsernameRESTService','$q',function(UsernameRESTService,$q){
    return{
        usernameExists: function(username){
            var deferred = $q.defer();
            deffered.resolve(true);
            return deferred.promise;
        }
}}])

Directive:

angular.module('myApp.commonDirectives', ['myApp.services']).directive('username', ['UsernameValidationHelper','$q',function(UsernameValidationHelper,$q){
    
    return {
        restrict: 'A',
        require: "ngModel",
        
        link: function(scope, element, attrs, ctrl) {
            ctrl.$asyncValidators.username = function(value){
                var deferred = $q.defer();
                    bob= UsernameValidationHelper.usernameExists("bob");
                    defer.resolve();
                    return deferred.promise;
                }
            }
        }
}])

At the moment I'm just trying things out given my lack of knowledge of promises.

When calling bob= UsernameValidationHelper.usernameExists("bob"); I get a Type Error: Undefined is not a function

What am I doing wrong?

1

There are 1 best solutions below

0
On

Three injections, but only two (different) params in directive:

['UsernameRESTService','CarrierValidationHelper','$q',function(UsernameValidationHelper,$q){}]