Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access image properties after angular ng-repeat?

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> ...

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?

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

like image 360
wonglik Avatar asked Jan 24 '13 18:01

wonglik


People also ask

Does ng-repeat create a new scope?

Directives that Create Scopes In most cases, directives and scopes interact but do not create new instances of scope. However, some directives, such as ng-controller and ng-repeat, create new child scopes and attach the child scope to the corresponding DOM element.

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What can I use instead of NG-repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do I use nested ng-repeat?

The ng-repeat directive instantiates a template once per item from a collection like array, list etc. In this blog, I am using nested ng-repeat to show the country list along with their city name. Create a project and install AngularJS from NuGet Package, add a script file to the project and write Angular code.


1 Answers

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

and you'd use it like so:

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

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

like image 165
Ben Lesh Avatar answered Oct 05 '22 18:10

Ben Lesh