如何获得一个动态创建的元素的宽度与Angularjs $ comiple [英] How to get width of a dynamically created element with $comiple in Angularjs
问题描述
所以我想创造一个角度元素并追加到DOM:
So I want to create an element in angular and append it to DOM :
//in my directive I've created an element as bellow :
el = $compile('<span>Hello dynamically</span>')($scope);
// and then I appended it to my DOM like this :
myDomElement.append(el);
到目前为止好,没有错误什么都没有。
So far so good , no errors no nothing .
所有我要找的是如何获得的的宽度的这个报,我在我的指令已经创建了?
All I'm looking for is to how to get the width of this el that I've created in my directive ?
通常,当我想要得到一个元素的宽度,我会得到的 offsetWidth 的该元素的,
Normally when I want to get width of an element I'll get the offsetWidth of that element ,
我在寻找这样的事情:
el = $compile('<span>Hello dynamically</span>')($scope);
var width = el.offsetWidth ; // I want the width here !!
console.log(width); // will log 0 !!!!!!
console.log(el); // will show an Object that has a **offsetWidth:134** property !!!!!!!
myDomElement.append(el);
但是,当我登录这一点,我总是 0 ,这是奇怪,因为当我看着我的开发人员工具铬我可以看到的 offsetWidth 的不 0 !
But when I log this , I always get 0 , it's weird because when I look at my developer tools in chrome I can see that offsetWidth is not 0 !
推荐答案
做到这一点:
el = $compile('<span>Hello dynamically</span>')($scope);
myDomElement.append(el);
var width = el.offsetWidth ; // I want the width here !!
console.log(width);
首先意味着附加元素DOM,然后得到它的宽度
Means first append the element to DOM , then get it's width
这篇关于如何获得一个动态创建的元素的宽度与Angularjs $ comiple的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!