Skip to content

Instantly share code, notes, and snippets.

@killerchip
Created December 31, 2017 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save killerchip/a92f52a20eac7f69692a4bcc6483454c to your computer and use it in GitHub Desktop.
Save killerchip/a92f52a20eac7f69692a4bcc6483454c to your computer and use it in GitHub Desktop.
Angular tip: How to add a class to a component from "inside".

Component adds class to itself

When defining a component you can define a class (and other attribute) on its own host element.

For example if you define the <my-component></my-component component, you can define it to have a class like <my-component class="my-component-class"> directly from compoent code. So you are not dependent on the "user" of this component to define the class.

This can be done with the @HostBinding

export class MyComponent {

  @HostBinding('attr.class') cssClass='my-component-class'

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment