Marco Pereira Marco Pereira - 4 months ago 16
AngularJS Question

Calling Services in a custom directive

I have a custom directive that was working fine, untill i added some services to it, is giving me a "Error: ng:areq Bad Argument" cant figure it out why.

My directive:

angular.module('myApp')
.directive('modalContent',[
'$uibModal',
'UserService'],
function($uibModal, UserService){
return {
restrict: 'A',
priority: 100,
link: function($scope, element, attrs) {
element.on( 'click', function( evt ){

console.log("Click and Stop");

evt.preventDefault()
evt.stopImmediatePropagation();

$scope.$apply(function(){
console.log("call Modal");
});

});
}
};
});

Answer

Try initializing the angular module before creating the directive:

angular.module('myApp',[]);

After that, you can use your code.


Edit:

The error is due to a syntax error, you have an extra ] after 'UserService' the correct definition of the directive is:

angular.module('myApp')
    .directive('modalContent',[
        '$uibModal',
        'UserService',
        function($uibModal, UserService){
        return {
            restrict: 'A',
            priority: 100,
            link: function($scope, element, attrs) {
                element.on( 'click', function( evt ){

                    console.log("Click and Stop");

                    evt.preventDefault()
                    evt.stopImmediatePropagation();

                    $scope.$apply(function(){
                        console.log("call Modal");
                    });

                });
            }
        };
    }]);

Also notice a change in the end: }]); instead of });

Comments