I want to use HostBinding
decorator in order to add a class, which cannot be hardcoded like @HostBinding('class.myClass')
.
I know there is a possibility to bind it to the whole class
attribute like @HostBinding('class')
, but that will obviously reset all classes added directly to my host element in the place where it is used.
So is it possible to use HostBinding
, but only to add one class and preserve all previously added classes in html?
Currently I ended up with uglier solution:
@Component({
...
})
class MyComponent implements OnInit {
constructor(private hostElement: ElementRef,
private renderer: Renderer2) { }
ngOnInit() {
const randomClass = `dynamic-${Math.floor(Math.random() * 10) + 1}`;
this.renderer.addClass(this.hostElement.nativeElement, dynamicClass);
}
}