javascript AngularJS 指令中的元素高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27894755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:08:47  来源:igfitidea点击:

Element height from within an AngularJS directive

javascriptjquerycssangularjs

提问by Charles Sounder

I'm trying to get the height of elements in a simple AngularJS app. See below. What am I doing wrong? The height should be different as the lines wrap, but I get 20 reported back to me regardless of what I input in the "labels" array.

我试图在一个简单的 AngularJS 应用程序中获取元素的高度。见下文。我究竟做错了什么?当线条环绕时,高度应该不同,但无论我在“标签”数组中输入什么,我都会收到 20 报告给我。

The following code can be executed here, otherwise see below. http://js.do/code/49177

下面的代码可以在这里执行,否则见下文。 http://js.do/code/49177

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <base href="/">

  <title>height of element in angularjs</title>

  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">

  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular-route.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

  <script type="text/javascript">
    'use strict';

    var app = angular.module('heightApp', ['ngRoute', 'routing']);

    app.controller('heightCtrl', ['$scope', function ($scope) {
      $scope.labels = [
        'Hi there, I\'m a div.',
        'Me too, I\'m also a div.',
        'Can you see me, because I certainly can\'t see myself. I don\'t even know my own height. Isn\'t that just crazy?'
      ];

    }]);    

    angular.module('routing', []).config(['$routeProvider', function($routeProvider) {
      $routeProvider
        .when('/', {
          templateUrl: 'height.html',
          controller: 'heightCtrl'
        });
    }]);

    angular.module('heightApp').directive('reportMyHeight', function() {
      return function (scope, el, attrs) {
          alert('offsetHeight = ' + el[0].offsetHeight);
        }
    })
  </script>

</head>

<body ng-app="heightApp">

<div class="container">
  <div ng-view></div>
</div>

</body>

<script type="text/ng-template" id="height.html">
  <div class="row">
    <div class="col-sm-4" report-my-height ng-repeat="lbl in labels">
      {{ ::lbl }}
    </div>
  </div>
</script>

</html>

回答by PSL

You need to wait till the next digest cycle. When you do it right away in the directive the interpolations {{ ::lbl }}inside the ng-repeat would not have expanded yet. You can place it in a $timeoutturning off the applyDigest argument.

您需要等到下一个摘要周期。当您在指令中立即执行{{ ::lbl }}此操作时,ng-repeat 中的插值还没有扩展。您可以将它放在$timeout关闭 applyDigest 参数中。

i.e, example:

即,例如:

angular.module('heightApp').directive('reportMyHeight', function($timeout) {
   return function (scope, el, attrs) {

      $timeout(init, false);

      //Initialization
      function init(){
        console.log('offsetHeight = ' + el[0].offsetHeight,  el.html());
      }

    }
});

Plnkr

普林克

回答by Sergey Romanov

Another way to make sure you get the height of the element is to use watch.

确保获得元素高度的另一种方法是使用 watch。

angular.module('heightApp').directive('reportMyHeight', function($timeout) {
  return function (scope, el, attrs) {
    scope.$watch('lbl', function(newval, oldval){
      alert(newval + '\n\n' + 'offsetHeight = ' + el[0].offsetHeight);
    });
  }
})

It will only be triggered once since you use ::.

自从您使用后,它只会被触发一次::