【发布时间】:2018-04-24 01:38:14
【问题描述】:
您好,我有这个指令可以为元素添加背景颜色:
import {Directive, ElementRef, AfterViewInit, Input} from '@angular/core';
@Directive({
selector: '[bg-color]'
})
export class BgColorDirective implements AfterViewInit {
private el: HTMLElement;
@Input('bg-color') backgroundColor: string;
constructor(el: ElementRef) {
this.el = el.nativeElement;
}
ngAfterViewInit() {
this.el.style.backgroundColor = this.backgroundColor;
}
}
但就我而言,我在另一个组件 ngx-slick 中使用它,并且该组件总是更改样式然后覆盖我的指令结果,那么有没有办法在覆盖后应用我的指令?
【问题讨论】:
-
什么时候改颜色?
-
看看 observable 和 observer 对象,您应该能够在组件渲染后(或任何时候)应用触发器来改变颜色
-
@Aravind:我不希望这个动态改变颜色,我只想用来建立颜色,但如果我在我的元素中使用
style="backgrond-color..."ngx-slick覆盖这个规则,所以我认为使用指令是最好的解决方案 -
@FabioGuerrazzi,你能给我举个例子吗?
-
是的,我正在寻找它,stackoverflow.com/questions/41274603/…
标签: angular angular2-directives angular-directive