Getting aspect ratio of an element in ng-repeat returns undefined in Angularjs

I am trying to get the height of the current element in ng-repeat.

When I register element

, I can see the whole object with the property in the console clientHeight

. But if I log element[0].clientHeight

separately the console returns undefined

..

What am I doing wrong?

Js

app.directive('photo', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            console.log( element[0].clientHeight);
        }
    }
});

      

Html

<div ng-repeat="media in mediaScope" class="gallery_media col-xs-6">
    <img photo ng-src="{{ media.url }}">
</div>

      

+3


source to share


2 answers


You need to set an event onload

on this element that will get called when the img fro attribute is loadedsrc

Directive



app.directive('photo', function(){
    return {
        restrict: 'E',
        link: function(scope, element, attrs) {
            element.on('load', function(){
                console.log( element[0].clientHeight);
            });
        }
    }
});

      

+1


source


Pankajparkar's answer is correct, but the limit parameter should be set to "A".

restrict: 'A'

      



http://jsfiddle.net/andresrondan/kaL5eot0/1/

0


source







All Articles