{"id":1195,"date":"2021-06-08T09:17:32","date_gmt":"2021-06-08T07:17:32","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1195"},"modified":"2022-06-08T17:54:40","modified_gmt":"2022-06-08T15:54:40","slug":"how-to-add-class-to-your-host-element-in-angular","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/06\/08\/how-to-add-class-to-your-host-element-in-angular\/","title":{"rendered":"How to add class to your host element in Angular"},"content":{"rendered":"\n<p>There are different ways to solve that, depending of the situation or what we want to achieve. <\/p>\n\n\n\n<h5>Solution A : <em><span class=\"has-inline-color has-vivid-red-color\">(Static class)<\/span><\/em><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component({\n   selector: 'app-component',\n   template: '&lt;ng-content&gt;&lt;\/ng-content&gt;',\n\n})\nexport class App implements OnInit {\n  constructor(private cdRef:ChangeDetectorRef) {}\n  \n  someField: boolean = false;\n\n  @HostBinding('class.someClass') someField: boolean = false;\n\n  ngOnInit() {\n    this.someField = true; \/\/ set class `someClass` on `&lt;app-component&gt;`\n    \/\/this.cdRef.detectChanges(); \n  }\n}<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Solution B : <span class=\"has-inline-color has-vivid-red-color\"> <em>(dynamic class)<\/em><\/span><\/h5>\n\n\n\n<p>If you want to add a dynamic class to your host element, you may combine your&nbsp;<code>HostBinding<\/code>&nbsp;with a getter as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@HostBinding('class') get class() {\n    return aComponentVariable \/\/ the name of the class. \n}<\/code><\/pre>\n\n\n\n<h5>Example:<\/h5>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>HTML: <\/em> <em>app.component.html<\/em><\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h3&gt;Pick the color of the text component&lt;\/h3&gt;\n&lt;input type=\"radio\" name=\"headingColor\" &#91;value]=\"'Blue'\" id=\"Blue\" &#91;(ngModel)]=\"headingColor\"&gt;&lt;label for=\"Blue\"&gt;Blue&lt;\/label&gt;\n&lt;input type=\"radio\" name=\"headingColor\" &#91;value]=\"'Red'\" id=\"Red\" &#91;(ngModel)]=\"headingColor\"&gt;&lt;label for=\"Red\"&gt;Red&lt;\/label&gt;\n&lt;input type=\"radio\" name=\"headingColor\" &#91;value]=\"'Green'\" id=\"Green\" &#91;(ngModel)]=\"headingColor\"&gt;&lt;label for=\"Green\"&gt;Green&lt;\/label&gt;<\/code><\/pre>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>TYPESCRIPT:<\/em> <em>app.component.<\/em>ts<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component, HostBinding } from '@angular\/core';\n\n@Component({\n  selector: 'my-app',\n  templateUrl: '.\/app.component.html',\n  styleUrls: &#91; '.\/app.component.css' ]\n})\nexport class AppComponent  {\n  headingColor: string = 'Black';\n\n  @HostBinding('class') get HeadingClass() {\n    return this.headingColor\n  }\n}<\/code><\/pre>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><em>STYLESHEET:<\/em>  <em>app.component.<\/em>css<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>:host.Black{\n  color: Black;\n}\n\n:host.Green {color: Green}\n\n:host.Blue {color: Blue}\n\n:host.Red {color: Red}<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Solution C :<em><span class=\"has-inline-color has-vivid-red-color\">  (Use of ElementRef property)<\/span><\/em><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import {Component, OnInit, ElementRef} from '@angular\/core';\n\n@Component({\n  selector: 'my-app',\n  template: '&lt;ng-content&gt;&lt;\/ng-content&gt;'\n})\nexport class AppComponent implements OnInit { \n\n private currentIconClass: string;\nget hostElement(): HTMLElement {\n    return this._elementRef.nativeElement;\n  }\n  constructor(public _elementRef: ElementRef&lt;HTMLElement&gt;) {\n  }\n ngOnInit() {\n    this.setIconClass(this.hostElement.textContent);\n  }\n  private setIconClass(iconClass: string): void {\n\n    if (!iconClass) {\n      console.warn('Missing iconClass!');\n      return;\n    }\n\n    this.clearElement();\n\n    if (this.currentIconClass) {\n      this.hostElement.classList.remove(this.currentIconClass);\n    }\n\n    this.currentIconClass = iconClass;\n\n    this.hostElement.classList.add(this.currentIconClass);\n  }\n   private clearElement(): void {\n    this.hostElement.textContent = undefined;\n  }\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are different ways to solve that, depending of the situation or what we want to achieve. Solution A : (Static class) Solution B : (dynamic class) If you want to add a dynamic class to your host element, you may combine your&nbsp;HostBinding&nbsp;with a getter as Example: HTML: app.component.html TYPESCRIPT: app.component.ts STYLESHEET: app.component.css Solution C [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1965,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,83],"tags":[331,330,333,334,332],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1195"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":4,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1968,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions\/1968"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1965"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}