Marco Santos
Marco Santos

Reputation: 1005

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");
                    });

                });
            }
        };
    });

Upvotes: 0

Views: 341

Answers (1)

Freddy Garcia Cala
Freddy Garcia Cala

Reputation: 414

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 });

Upvotes: 1

Related Questions