我想知道我的指令模板中<div>的宽度,并存储在变量中我如何实现</div>

时间:2015-02-25 07:10:39

标签: javascript html css angularjs

知道div的宽度

我想知道模板内的宽度,并存储在变量中我该如何实现

pp.directive('skillSlider',function()
		{
	return{
		template:'<div class="progress"></div>'
		}	
		});


----------
how to calculate the width of the div inside the directive template

2 个答案:

答案 0 :(得分:0)

你可以使用链接功能访问元素,使用它可以获得div的宽度。

pp.directive('skillSlider',function()
    {
       return{
              template:'<div class="progress"></div>',

              link:function (scope, element, attrs) {
                console.log(element.width());
               })   
    }
 });     

答案 1 :(得分:0)

您可以使用angular.element例如:

执行此操作
angular.element(document.getElementById(id)).clientWidth;
angular.element(document.querySelectorAll('.class')[0]).clientWidth;

或者如果你加载了jQuery

angular.element($('#id').width();
angular.element($('.class')[0]).width();