0
我有一个自定义指令,我想要获取我的img src,它来自作用域变量。然后,我会检查这个img大小和屏幕大小,以保持此图像或更改它的来源。不能在自定义指令中获取img源码
,但我不能够连得在首位的图像源,这是我的指令:
function repSrc($timeout) {
return {
restrict: 'A',
scope: true,
link: function(scope, element, attrs) {
//Get image source here
console.log(attrs.repSrc);
}
}
}
而且我的html:
<li ng-repeat="obj in vm.imageList">
<img rep-src="{{obj.imgDetail}}" />
</li>
我甚至试图用指令控制器内,试图范围值从scope: true
更改为:
scope: {
localImg: '@repSrc'
}
但是所有这些克在attrs
值上给我一个空字符串。我究竟做错了什么?
您可以通过'scope.localImg'访问数据。你有没有用你的第二个范围定义来尝试? –
@SimonSchüpbach是的,但仍然只有一个空字符串。即使我在$ timeout内做了这个 – celsomtrindade
你不需要使用'setTimeout',而是检查是否在调用指令链接方法之前定义了obj.imgDetail。 –