5

我正在尝试获取从服务器中提取的图像的图像宽度和高度,以便为其应用适当的样式。所以我使用 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)。

4

1 回答 1

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.
         });
     }
   };
});

你会像这样使用它:

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

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

于 2013-01-24T18:34:42.950 回答