1

I'm using angular 1.4.x.

I'm making a custom directive that checks weather a field is unique on the server (the field is called "jmbg"). I have the following code:

(function() {
angular
    .module('app')
    .directive('uniqueJmbg', uniqueJmbg);

uniqueJmbg.$inject = ['$q', '$http'];
function uniqueJmbg($q, $http) {
    restrict: 'A',
    require: 'ngModel',
    link: function(scope, elem, attrs, ngModelCtrl) {
        ngModelCtrl.$asyncValidators.uniqueJmbg = function(modelValue, viewValue) {
            var value = modelValue || viewValue;

            return $http.get('/server/users/' + value)
                .then(function resolved() {
                    return $q.reject('exists');
                }, function rejected() {
                    return true;
             });
        };
    }
}
})();

I am using the directive in HTML in the following way:

<input class="form-control" type="text" id="jmbg" name="jmbg" ng-model="rad.radnik.jmbg" ng-model-options="{ updateOn: 'default blur', debounce: {'default':400, 'blur':0 } }" unique-jmbg/>

In case it matters, I'm using my controllers with the controllerAs syntax. Now, what happens is that the file containing my uniqueJmbg definition never loads (I can't see it in the browser debugger). If I move my code to a component which does load the app stops working (and there are no errors in the console), so there is no way for me to debug this.

Any idea what might be so wrong I can't even access the code in the browser?

1
  • This may sound a bit silly, but are you loading the directive.js file in your index.html file? Commented Sep 6, 2015 at 19:58

1 Answer 1

1

Add dependencies to the module

angular
    .module('app', [])
    .directive(...

And you need to return an object from the module, so the correction would be:

function uniqueJmbg($q, $http) {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, elem, attrs, ngModelCtrl) {
            ...
        }
    };
}
Sign up to request clarification or add additional context in comments.

5 Comments

I think your first correction is wrong—from the wording, it sounds like the OP defines the app module elsewhere, in which case the one-argument call to module is correct. The second part of your answer is accurate, though.
It is defined elsewhere, that's why you need dependencies for the module (if there are any, it can stay as an empty array). So you can inject them there as angular.module('app.whatever.module', ['app.whatever.dependency']).directive()
No no, the OP is using the single-argument version of module to act as a getter. In that case the two-argument version is wrong because it will attempt to redefine a new module with the same name.
I can see what you're saying, I agree, but I have no idea if he named his main module App, and he's using app as this one. In my previous comment I just gave an example for the nested modules.
That was it. I can't believe I wasted an hour not realizing I didn't return the directive configuration object.

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.