Angular dynamic classes using ngClass

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
It is! You're welcome
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday, we looked at dynamic form fields, and it made me think about custom classes in Angular.
You might want to add class1 based on a condition or class2 if the condition is not met.
How can we achieve such a thing? Well, that is where the ngClass comes in handy.
Before we make things dynamic, let's first look at how it looks without any conditions.
If you want to work along with me, I'm using this branch as the starter template.
Open up the app.component.html file.
Add the following.
<div [ngClass]="'m-8 p-8 bg-blue-500'">Hi, I'm the div</div>
This will add three classes to this div. And it will look something like this:

But now, let's introduce a dynamic class based on a condition.
<div [ngClass]="[color ? 'bg-blue-500' : 'bg-purple-500']">Hi, I'm the div</div>
This is a reversed boolean check, so we check if the color variable is true, it will be blue, else purple.
Now we need to add this variable to our app.component.ts file.
export class AppComponent {
color: boolean = false;
}
If we run this, we get a purple block, which is correct. Let's add a simple click function to toggle the state.
<div (click)="color = !color" [ngClass]="[color ? 'bg-blue-500' : 'bg-purple-500']">
Hi, I'm the div
</div>
This will toggle the color variable to the opposite.
And now we should see the color change if we click it.

And with that, we've learned how to change classes dynamically in Angular.
You can find today's code on the following GitHub repo.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter