2014-09-24 108 views
0

我無法獲取呈現元素的高度。它返回null。我需要元素來檢查它自身的高度,而不需要用戶交互。AngularJS - 無法獲取元素的高度

HTML

<span ng-if="contentHeight($element)" ></span> 

JS

$scope.contentHeight = function(element){ 
     console.log($(element).height()); 
     return true; 
    }; 
+0

您的元素實際上是否顯示?測量不可見的元素有點棘手。 – 2014-09-24 13:12:25

+0

$ element從哪裏來,試圖記錄$(元素)? – graphefruit 2014-09-24 13:12:41

+0

@Bartdude是顯示 – user1477955 2014-09-24 13:13:56

回答

2

,如果你是因爲它把元素插入指令本身在這裏使用一個指令,它可能更好地爲你。

HTML

<span class="directive"></span> 

JS

app.directive('directive', function(){ 
    return{ 
     restrict: 'C', 
     link: function(scope, element, attrs){ 
      console.log($(element).height()); 
     } 
    } 
}); 

我不知道你想要做什麼,但可能會解決您的問題:)

編輯:添加文檔! https://docs.angularjs.org/guide/directive