I have successfully used @HostBinding in my Angular 6 app to apply properties to the host component, as in applying a class when a variable is true:
@HostBinding('class.compact-ui') isCompact;
Now, however, I need to assign one of 4 possible classes based on the model of a select menu. For example, user can red, blue, or green.
I suppose I can use multiple host bindings for when any of the colors are true:
@HostBinding('class.green-ui') uiColor === 'green';
But that seems wrong. What's the proper way to do this?
To assign multiple classes to a host you can do this:
For your case, you could do something like this:
Note that above function logic can be written in a single line, like this:
Also, you might consider using a Redux store (e.g.
@ngrx/store) for those values (isCompact,color) if you need them in multiple places in your application.