gpt4 book ai didi

javascript - angular ng-repeat后如何访问图像属性?

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:55 25 4
gpt4 key购买 nike

我正在尝试获取从服务器中提取的图像的图像宽度和高度,以便为其应用适当的样式。所以我使用 ng-repeat 来填充模板:

<div ng-repeat="feed in feeds"> 
<div class="span2" >
<img ng-src='{{feed.image_url}}'>
</div> ...

我的问题是如何访问 img 对象以便更改父 div 类?或者也许 angular.js 做那样的事情的方式是什么?

编辑:更具体。我想访问 img 对象以获取其宽度和高度以计算其大小以将样式应用于父 div(当前类 span2)。

最佳答案

我不知道你想做什么,但这听起来像是指令的工作。

app.directive('styleParent', function(){ 
return {
restrict: 'A',
link: function(scope, elem, attr) {
elem.on('load', function() {
var w = $(this).width(),
h = $(this).height();

var div = elem.parent();

//check width and height and apply styling to parent here.
});
}
};
});

你会像这样使用它:

<img ng-src="imageFile" style-parent/>

编辑:如果您不使用 jquery,这会有所不同。

关于javascript - angular ng-repeat后如何访问图像属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com