角度ng-repeat后如何访问图像属性?

won*_*lik 5 javascript angularjs

我试图获取从服务器拉出的图像的图像宽度和高度,以便为它应用适当的样式.所以我使用ng-repeat来填充模板:

<div ng-repeat="feed in feeds"> 
<div class="span2" >
    <img ng-src='{{feed.image_url}}'>
</div> ...
Run Code Online (Sandbox Code Playgroud)

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

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

Ben*_*esh 14

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

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.
         });
     }
   };
});
Run Code Online (Sandbox Code Playgroud)

你会像这样使用它:

<img ng-src="imageFile" style-parent/>
Run Code Online (Sandbox Code Playgroud)

编辑:如果你不使用jquery,这会有所不同.