当得到一个角指令的宽度是多少? [英] When to get the width of an Angular directive?

查看:93
本文介绍了当得到一个角指令的宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我怎么能得到一个待编译div的长度控制器和/或指令。长度,想在这种情况下,而不诉诸 $超时?还是有一个事件告诉我,角的工作全部做完,我可以去得到它的高度/宽度?​​

When and how can I get the length of a to-be-compiled div in Controller and/or Directive, the .length-wanted in this case, without resorting to $timeout? Or is there an event telling me that Angular's work is all done and I could go get its height/width?

<div ng-controller="testCtrl">
    <div get-length>
        <div class="length-wanted" ng-bind="arr"></div>
    </div>
</div>

示范这里: http://jsfiddle.net/luxiyalu/tm54k4je/

推荐答案

因为它可能是它的大小可能与调整大小的改变,我最终与去:

Since it's possible that its size might change with resize, I eventually went with:

<div ng-controller="testCtrl">
    <div get-length>
        <div class="height-wanted" height="data.height" ng-bind="arr"></div>
    </div>
</div>

指令:

app.directive('lengthWanted', function() {
  return {
    restrict: 'C',
    scope: {height: '='},
    link: function (scope, element) {
      scope.$watch(function() {
        return element.height();
      }, function(newVal) {
        scope.height = newVal;
      });
    }
  };
});

当然,这是检查元素的每 $消化的高度。您可以通过在范围,如果它设置为false返回观看的功能存储调整性能优化这一点。

Of course, this is checking the height of the element with every $digest. You could optimise this by storing a resized property in scope and returning the watched function if it's set to false.

这篇关于当得到一个角指令的宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆