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
- Scope of the element
- The element itself (jquery/mini jquery wrapped)
- Attribute set of the element
- 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.