我需要使用$q我的指令的链接功能.我需要它来包装由一个参数重新调整的可能的promise(参见下面的例子).但是,我不知道如何将$q依赖项传递给此函数.
angular.module('directives')
.directive('myDirective',function() {
return {
scope: {
onEvent: '&'
}
// ...
link: function($scope,$element) {
$scope.handleEvent() {
$q.when($scope.onEvent()) {
...
}
}
}
}
}
解决方法
只需将其添加为您的指令的依赖项,$q将在链接函数中使用.这是因为JavaScript的
closures.
以下是基于您的代码的示例.
angular.module('directives')
.directive('myDirective',['$q',function($q) {
return {
scope: {
onEvent: '&'
}
// ...
link: function($scope,$element) {
$scope.handleEvent() {
$q.when($scope.onEvent()) {
...
}
}
}
}
}])