相关疑难解决方法(0)

nativeElement.classList.add()替代方案

我正在尝试以角度2创建按钮组件.在主机上,我必须设置动态生成的css类名.(取决于绑定属性)

主机上的'[ngClass]'不起作用.

我已经读过使用elementRef.nativeElement.classList.add(value)也不是最好的方法,因为webworkers(或左右)不支持classList

在主机上动态生成类的最佳选择是什么?

@Component({
    selector: '[md-button]',
})
export class MdButton {
    color_: string;

    @Input
    set color() {
        this.color_ = value;
        if (this.elementRef !== undefined) {
            this.elementRef.nativeElement.classList.add('md-' + this.color_);
        }   
    }

    get color(): string {
        return this.color_;
    }

    constructor(public elementRef: ElementRef){}
} 
Run Code Online (Sandbox Code Playgroud)

typescript angular

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

angular ×1

typescript ×1