Skip to content
the poor coder

Angular add class dynamically

Poor Man
Published
Reading time
2 min read
On this page

Angular Add Class Dynamically#

If you want to add a class to an element dynamically in Angular, there are a few different ways to do it.

Method 1: ngClass directive#

The ngClass directive allows you to add and remove CSS classes based on conditions. You can use it like this:

Text
<div [ngClass]="{'my-class': shouldAddClass}">Hello World</div>

In this example, the "my-class" class will be added to the div element if the "shouldAddClass" variable is true.

Method 2: Renderer2.addClass()#

If you need to add a class to an element in TypeScript code, you can use the Renderer2 class that is provided by Angular. Here's an example:

Text
import { Component, ElementRef, Renderer2 } from '@angular/core';

@Component({
  selector: 'app-root',
  template: '<div #myDiv>Hello World</div>'
})
export class AppComponent {
  constructor(private elementRef: ElementRef, private renderer: Renderer2) {}

  ngAfterViewInit() {
    const div = this.elementRef.nativeElement.querySelector('#myDiv');
    this.renderer.addClass(div, 'my-class');
  }
}

In this example, we inject the ElementRef and Renderer2 services into our component's constructor. Then, in the ngAfterViewInit method (which is called after the view has been initialized), we use the Renderer2.addClass method to add the "my-class" class to the div element.

Method 3: HostBinding decorator#

If you need to add a class to the host element of a component, you can use the HostBinding decorator. Here's an example:

Text
import { Component, HostBinding } from '@angular/core';

@Component({
  selector: 'app-root',
  template: 'Hello World'
})
export class AppComponent {
  @HostBinding('class.my-class') shouldAddClass = true;
}

In this example, we use the HostBinding decorator to bind the "shouldAddClass" property to the "class.my-class" attribute of the host element (which is the app-root element in this case). When the "shouldAddClass" property is true, the "my-class" class will be added to the host element.

Conclusion#

These are three different ways to add a class to an element dynamically in Angular. Choose the one that fits your use case the best!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC