计算AngularJS中DOM的动态高度

时间:2015-04-01 13:09:57

标签: angularjs

当DOM Load完成时,我想测量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 Load **

之前触发高度

2 个答案:

答案 0 :(得分:1)

我会尝试像这样使用$ timeout

未经测试

    $('.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);  
    })

答案 1 :(得分: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)
           
        })
         
    }
  }
}])