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]' on host does not work.
I've read that using elementRef.nativeElement.classList.add(value) is not the best way either, because classList is not supported on webworkers (or so)
What are my best options to generate the class dynamically on host?
@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){}
}
@Component({
selector: '[md-button]' //,
// host: { '[class]': 'classList()' }
})
export class MdButton {
color_: string;
// classList() {
// return 'md-' + this.color_;
// }
@Input
set color() {
this.color_ = value;
// if (this.elementRef !== undefined) {
// this.elementRef.nativeElement.classList.add('md-' + this.color_);
// }
this.renderer.setElementClass(this.elementRef, 'md-' + this.color_, true);
}
get color(): string {
return this.color_;
}
constructor(public elementRef: ElementRef, private renderer: Renderer2){}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With