2015-05-02 48 views
3

我試圖獲取ng-repeat中當前元素的高度。獲取Angularjs中未定義的ng-repeat中元素的比例

當我登錄時element我可以在控制檯中看到具有clientHeight屬性的整個對象。但如果我記錄element[0].clientHeight分別控制檯返回undefined ..

我在做什麼錯了?

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> 
+2

是吧'undefined'或'0'?應該是'0',因爲'img'還沒有加載 –

+0

@alliswell看看我的答案,會幫助你.. –

回答

1

您需要在元素上設置onload事件,這將讓來來往往的img時調用src屬性加載

指令

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

謝謝@pankajparkar! :) – Alliswell

+0

@阿里斯韋爾很高興幫助你的男人..感謝 –

0

pankajparkar響應是正確的,但限制參數應設置爲'A'。

restrict: 'A' 

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

+0

默認情況下'restrict:'A'' only ..爲什麼你想指定它.. ..你有相同的代碼..我發佈.. –