如何角NG重复后访问图片属性? [英] How to access image properties after angular ng-repeat?

查看:107
本文介绍了如何角NG重复后访问图片属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让图像宽度和图像高度,我从服务器中拉出,以申请合适的风格吧。所以我用NG-重复填写模板:

I am trying to get image width and height of an image that I pull from server in order to apply proper style for it. So I am using ng-repeat to fill template :

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

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

My question is how do I access img object so I can change parent div class? Or maybe what is the angular.js way of doing thing like that?

编辑:更加指定。我想访问IMG对象,以获取其宽度和高度来计算其规模应用样式父格(目前与类SPAN2)。

Edit : To be more specified. I want to access img object to get its width and height to calculate its size to apply style to parent div (currently with class span2).

推荐答案

我不知道你想做什么,但它听起来像一个指令工作。

I don't know what you're trying to do, but it sounds like a job for a directive.

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,这将改变一点点

If you're not using jquery, this will vary a little.

这篇关于如何角NG重复后访问图片属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆