问题描述
当我怎么能得到一个待编译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>
示范这里:
推荐答案
因为它可能是它的大小可能与调整大小的改变,我最终与去:
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.
这篇关于当得到一个角指令的宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!