ng-repeat中的角度打印元素高度和宽度

时间:2014-04-04 16:10:22

标签: angularjs angularjs-directive angularjs-ng-repeat

基本上标题是......

<td data-ng-repeat="j in [0,1,2,3,4,5,6,7,8,9,10]">
    Height:{{this.height}}, Width:{{this.width}}
</td>

但是this引用了$ scope而不是<td>本身。

1 个答案:

答案 0 :(得分:1)

尝试指令方法,也许是这样的?将html更改为

<td data-ng-repeat="j in [0,1,2,3,4,5,6,7,8,9,10]" dimensions>
    Height:{{height}}, Width:{{width}}
</td>

.directive('dimensions', function() {
    return {
        link: function(scope, elem) {
            scope.height = elem[0].offsetHeight;
            scope.width = elem[0].offsetWidth;
        }
    }
});