inject dependency into a link function in a Directive Angularjs?

Asked

Viewed 53 times

0

Personal I am making a Directive(component) that needs a dependency $compile, I am doing the operations on the directive link and need the $compile inside of it only that I am not managing to inject the dependency someone there has already gone through something similar and managed to solve?

(function () {
    'use strict';

    angular
        .module('calendar', ['calendar.tmpl'])
        .directive('calendar', calendar);

    function calendar() {
        var directive = {
            restrict: 'E',
            scope: {
                getData: '&',
                tipo: '@',
                iconSize: '@',
                modelo: '=',
                eventosData: '='
            },
            templateUrl: function (element, attrs) {
                if (attrs.tipo == 'calendar') {
                    return 'calendar.html';
                } else if (attrs.tipo == 'datapicker') {
                    return 'datapicker.html';
                }
            },
            link: linkFunc
        };

        return directive;
    }
    function linkFunc(scope){
  ***//quero injectar o $compile aqui***
   }
}());

1 answer

0


Try it like this:

You can also try to pass the: calendar($compile) { ... } If it doesn’t work, I believe it should solve the problem:

(function () {
    'use strict';

    angular
        .module('calendar', ['calendar.tmpl'])
        .directive('calendar', ['$compile', function($compile) {

        var _scope = this,
        directive = {
            restrict: 'E',
            scope: {
                getData: '&',
                tipo: '@',
                iconSize: '@',
                modelo: '=',
                eventosData: '='
            },
            templateUrl: function (element, attrs) {
                if (attrs.tipo == 'calendar') {
                    return 'calendar.html';
                } else if (attrs.tipo == 'datapicker') {
                    return 'datapicker.html';
                }
            },
            link: _scope.linkFunc
        };

    return directive;

    this.linkFunc = function(scope){
      ***//quero injectar o $compile aqui***
    }

   }]);


}());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.