I have Angular 6.0.0 application. Need to dynamically append classes to html elements. Ideally, this should be implemented via directive. Currently I have something like this and it works OK.
<div class="class1 prefix-{{variable}}"></div>
I want to make the prefix reusable and I am trying to reach the better result by using a directive:
html:
<div class="class1" [appendBusinessLogicClass]="variable"></div>
appendBusinessLogicClass.directive.ts:
export class AppendBusinessLogicClass {
readonly prefix = 'prefix';
@HostBinding('class') class = ''; // this clears the html class1
@Input('appendBusinessLogicClass') set appendBusinessLogicClass(variable: string) {
this.class = this.prefix + '-' + variable;
}
}
But the HostBinding CLEARS class1
in the html code. But I'd like to APPEND 'prefix-rand'
to the class list.
Note, that we do not know variable
at the compile time.
Thanks