假设我有一个指令:directive:
<component>
<img ng-src='{{something}}' />
</component>
定义为:
app.directive("component", function() {
return {
scope: {},
restrict: 'E',
transclude: true,
template: "<a href='' ng-click='MyService.doThings()' ng-transclude></a>"
}
});
尽管我很努力,但我仍然不了解如何完成以下两个任务:
- 我该如何访问内部图像源路径?
- 我如何将此路径传递给服务
MyService
?(考虑一个灯箱包装器)
更新并解决:
app.directive("component", function(LightboxService) {
return {
restrict: 'E',
transclude: true,
replace: true,
template: "<a href='' ng-click='lb()' ng-transclude></a>",
link: function (scope, element, attrs) {
scope.lb = function () {
var src = $(element).find("img").attr("src");
LightboxService.show(src);
}
}
}
});