Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How can I dynamically set a CSS class list of an element in Angular from the class component?

I want to set a property as a string and add that dynamically into a certain DOM element as a class list value.

<div [className]="propertyVariable"></div>

And inside the class component:

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

@Component({
  selector: 'app-order',
  templateUrl: './order.component.html',
  styleUrls: ['./order.component.css'],
})
export class OrderComponent implements OnInit {
  propertyVariable: string;

  constructor() {}

  changePropertyVariable() {
    this.propertyVariable = 'class1 class2';
  }

  ngOnInit(): void {}
}

So far it does not add the class list am setting to that variable. How do I properly bind to that property?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use it like [class.className]="condition" to add className if condition is true. In this scenario className is hardcoded in template.

If you want your class name to be dynamic you can use this syntax: class="class1 class2 {{className}}".

For appending a list of classes you can use it like above only pass className = classList.join(' '); as the className.

So your template will be like: class="class1 class2 itemOneFromList itemTwoFromList ..."

I provided a stackblitz

about 4 years ago · Santiago Trujillo Report

0

Why not to use Angular ngClass directive with property binding to add classes dynamically?

<div [ngClass]="propertyVariable"></div>

https://angular.io/guide/property-binding

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!