I need to use $q
a link
function of my directive. I need it to wrap possible promise that is retuned by one of arguments (see the example below). I don't know however, how to pass $q
dependency to a this function.
angular.module('directives')
.directive('myDirective', function() {
return {
scope: {
onEvent: '&'
}
// ...
link: function($scope, $element) {
$scope.handleEvent() {
$q.when($scope.onEvent()) {
...
}
}
}
}
}
Just add it as a dependency on your directive and the $q will be usable in the link function. This is because of JavaScript's closures.
Below is an example based on your code.
angular.module('directives')
.directive('myDirective', ['$q', function($q) {
return {
scope: {
onEvent: '&'
}
// ...
link: function($scope, $element) {
$scope.handleEvent() {
$q.when($scope.onEvent()) {
...
}
}
}
}
}])
var module = angular.module('directives');
module.directive('myDirective', ['$q', function($q) {
...
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With