Do the parameters of a directive's link function rely on DI? Do the parameters of a directive's link function rely on DI? angularjs angularjs

Do the parameters of a directive's link function rely on DI?


No, the link function has a predefined set of parameters.

function link($scope, $element, attrs, ctrl) {    //Your method}

They are

  1. Scope of the element
  2. The element itself (jquery/mini jquery wrapped)
  3. Attribute set of the element
  4. Any controllers used in required


If you want to use DI with a directive (as I did), put the arguments to be injected in the directive factory function instead of the link function:

module.directive('name', function($timeout) {    return function(scope, element, attrs) {        ...    };});

To allow for minification, put the function argument in an array like you do for controllers:

module.directive('name', ['$timeout', function($timeout) {    return function(scope, element, attrs) {        ...    };}]);

See the current time example in the docs.

Edit: See here for a demo that injects the $timeout service. You can do the same thing when returning a directive (e.g. return {restrict: 'E', link: function() {...}}) object instead of a function.