Angular 6 HostBinding with multiple values?
Asked Answered
M

3

5

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?

Myrt answered 15/5, 2018 at 17:52 Comment(0)
M
7

To assign multiple classes to a host you can do this:

@HostBinding('class') classAttribute: string = 'classA classB';

For your case, you could do something like this:

@HostBinding('class') classAttribute: string;

// Example function that updates the classAttribute property
// You might handle it differently in your application
updateClassAttribute(color: string, isCompact: boolean) {
  const classes: string[] = [`${color}-ui`];

  if (isCompact) classes.push('compact-ui');

  this.classAttribute = classes.join(' ');
}

Note that above function logic can be written in a single line, like this:

this.classAttribute = [ ...isCompact ? ['compact-ui'] : [], `${color}-ui` ].join(' ');

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.

Matelote answered 25/10, 2018 at 13:42 Comment(0)
N
5

Note: This is for the latest versions of angular and may not work on older versions (as per the question title).

An object can also be returned which will add all the true values to the element and remove all the false values from the element.

Note: This also works with @HostBinding('style').

  /** The size of the button. */
  @Input() size: ButtonSize = 'md';

  /** The classes to attach to the element. */
  @HostBinding('class')
  get elementClasses() {
    return {
      'primary-button': true,
      [`primary-button--${this.size}`]: true,
      'primary-button--color': false,
      'other-class': this.isOtherClass()
    }
  }
Nitty answered 13/2, 2022 at 17:20 Comment(0)
S
1

You can do it with style too:

@HostBinding('style')
get style_binding()
{
    return { 
        display: 'grid',
        gap: this.config.internalGap,
        margin: this.config.margin,
        gridArea: this.config.area
    };
}
Soothsay answered 12/2, 2022 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.