nativeElement.classList.add() alternative

前端 未结 2 1013
傲寒
傲寒 2021-01-02 01:33

I\'m trying to create a button component in angular 2. At the host I have to set a dynamically generated css classname. (depending on binded property)

\'[ngClass]\'

2条回答
  •  醉话见心
    2021-01-02 02:17

    Thanks to Günter I've found the solution. This is his code but cleaned for anyone who can use it in the future.

    private color_: string;
    
    @Input()
    set color(value: string) {
        this.color_ = value;
    
        this.renderer.setElementClass(this.elementRef, 'md-' + this.color_, true);
    }
    
    get color(): string {
        return this.color_;
    }
    
    constructor(private elementRef: ElementRef, private renderer: Renderer) { }
    

提交回复
热议问题