2015-04-01 96 views
1

當DOM Load完成時,我想要測量AngularJS中DOM元素的高度。計算AngularJS中DOM的動態高度

myApp.directive('domHeight',['$timeout',function($timeout){ 
 
    return{ 
 
    restrict:'EA', 
 
    scope:true, 
 
    link:function(scope,element,attrs){ 
 
     $(document).ready(function(){ 
 
      $('.required-wrapper').load(function(){ 
 
       console.log($('.required-wrapper').height()); 
 
      }) 
 
      
 
     }) 
 
    } 
 
    } 
 
}])
**上面的代碼我觸發高度DOM加載之前**

回答

1

我會嘗試這樣

使用$超時未測試

$('.required-wrapper').load(function(){ 
     $timeout(function() { 
     //As far as I know there is no event which would notify you that the loaded DOM has rendered 
     //So I would use the timeout instead 
     console.log($('.required-wrapper').height()); 
     }, 100); 
    }) 
0

myApp.directive('domHeight',['$timeout',function($timeout){ 
 
    return{ 
 
    restrict:'EA', 
 
    scope:true, 
 
    link:function(scope,element,attrs){ 
 
     attrs.$observe(function(){ 
 
      $timeout(function(){ 
 
       $(document).ready(function(){ 
 
       $('.required-wrapper').load(function(){ 
 
       
 
       }) 
 
      
 
      }) 
 
       
 
       
 
      ),true) 
 
      
 
     }) 
 
      
 
    } 
 
    } 
 
}])