【发布时间】:2016-06-25 05:38:36
【问题描述】:
在给定的属性指令示例(即添加外观/行为的指令)中,我们对宿主元素的样式进行了相当简单的设置..例如
import {Directive, ElementRef } from 'angular2/core';
@Directive({
selector: '[myHighlight]'
})
export class HighlightDirective {
constructor(element) {
element.nativeElement.style.backgroundColor = 'yellow';
}
static get parameters(){
return [[ElementRef]];
}
我可以使用样式而不是设置样式吗?例如
@Directive({
selector: '[myHighlight]',
styles: [':host { background-color: yellow; }']
})
这似乎不适合我?
我正在做一些稍微复杂一些的事情,这导致了相当多的单一代码、设置大量样式、使用 AnimationBuilder 等等等。我觉得将它分成类和动画会更好一个 CSS。
ViewEncapsulation = 模拟/默认是否重要?
【问题讨论】:
-
虽然已经一年了,但为了后代,我在下面添加了答案。它既涉及使用组件作为指令,又涉及更改默认的 ViewEncapsulation。请看下面我的回答。 - 干杯
标签: angular angular2-directives