在指令中的数据绑定后获取宽度

时间:2014-12-08 02:08:23

标签: angularjs angularjs-directive

在指令中,我用ngRepeat编写一个元素,我需要在数据绑定后获得宽度。

我尝试使用post in link,但它也无法正常工作。

如何获得指令中的宽度?

2 个答案:

答案 0 :(得分:0)

link中的宽度不可用,因为该元素尚未成为DOM的一部分。但是,您可以观察宽度的任何变化,并在发生变化时进行处理。

scope.watch(function() {return element.style.width}, function() {
  // handle width change here
});

答案 1 :(得分:0)

compile: function () {
    return {
        pre: function ($scope, iElement, iAttrs) {
            function getWidth() {
                return iElement.width();
            }
        }

        $scope.$watch(getWidth, function (newWidth) {

            ...
        });
    }
}