我正在使用ng-repeat來創建項目。我想確定使用函數創建的每個元素的高度。Angular:從DOM中選擇元素
我知道如何將由ng-repeat創建的元素的索引傳遞給應該確定高度的函數,但是我在實際選擇該項目時遇到困難。
這是我使用的是什麼現在:
$scope.getItemHeight = function(index) { // index is index of element in ng-repeat
var itemHeight = angular.element('li').eq('+index+').offsetHeight;
return itemHeight;
};
但是,這並不因錯誤工作:Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite!
我也試過:
$scope.getItemHeight = function(index) {
var itemHeight = document.querySelectorAll('ul:nth-child('+index+')');
return itemHeight;
};
這將返回一個元素長度爲0,所以選擇器不起作用。
我在這裏錯過了什麼?
Codepen這裏:http://codepen.io/squrler/pen/LxsfE?editors=101
編輯: 我想什麼是不可能在這個時候。更多的信息在這裏:https://github.com/driftyco/ionic/issues/1691
唐你不想要「李」嗎?我想你會想'document.querySelectorAll('ul> li:nth-child('+ index +')');' – Ian
啊,對不起,錯字,你說得對,我正在尋找李。但不幸的是,它仍然返回一個長度爲0的元素。:( – Squrler
你不應該在指令中做任何DOM計算/操作嗎?這應該真正從控制器中抽象出來。 – Mark