【发布时间】:2016-05-17 03:03:17
【问题描述】:
有几种方法可以从 Angular2 中的元素获取样式:
假设你在一个组件或指令中:
1- 使用 nativeElement :
let color = elementRef.nativeElement.style.color;
但是,不建议这样做,因为如果您打算在未来使用类似 web worker 的东西,您不想直接访问 nativeElement。
2- 使用Ruler:
this.domAdapter = new browser.BrowserDomAdapter();
this.ruler = new Ruler( this.domAdapter );
this
.ruler
.measure( this.elementRef )
.then( ( rect ) => {
// Now we have access to height and width and left and top
} );
这很酷,但是尺子不会给我 color 或任何其他样式,基本上尺子只给了 Rectangle(基本上是 Element.getBoundingClientRect())。
另外,我们不能使用Renderer来get元素样式,它只提供set样式的方法。
【问题讨论】:
标签: angular