ItSolutionStuff.com

Angular Ngclass with Ternary Operator Example

By Hardik Savani • May 2, 2024
Angular

Hi,

Today, ngclass with ternary operator in angular is our main topic. if you have question about angular ngclass ternary operator then i will give simple example with solution. you will learn ternary operator ngclass angular 12. This article goes in detailed on angular 12 ngclass ternary operator. Let's get started with angular ngclass with ternary operator.

I will give you simple example of how to use ternary operator with ngclass in angular. you can use this example with in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13, angular 14, angular 15, angular 16 and angular 17 version app.

Let's see bellow example:

app.component.html

<h1>angular ngclass ternary operator</h1>

<button [ngClass]="status == 'Active' ? 'active-class' : 'inactive-class'">Button</button>

app.component.ts

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

@Component({

selector: 'my-app',

templateUrl: './app.component.html',

styleUrls: [ './app.component.css' ]

})

export class AppComponent {

name = 'Angular ' + VERSION.major;

status = 'Active';

}

app.component.css

.active-class {

color: green;

}

.inactive-class {

color: red;

}

Ouput:

i hope it can help you...

Tags: Angular
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Index of Ngfor in Angular?

Read Now →

Angular Check ngIf Null or Empty | Angular Check If Array is Empty

Read Now →

Angular NgForm Example | NgForm Directive In Angular

Read Now →

Angular NgModel Example | NgModel Directive In Angular

Read Now →

Angular NgStyle Example | NgStyle Directive In Angular

Read Now →

Angular NgClass Example | NgClass Directive In Angular

Read Now →

Angular NgSwitch Example | NgSwitch Directive In Angular

Read Now →

Angular NgIf Example | NgIf Directive In Angular

Read Now →

Angular NgFor Example | NgFor Directive In Angular

Read Now →

Angular NgIf Else | Ng If Else in Angular Example

Read Now →

Angular Ng-Container Example Tutorial

Read Now →

Angular Ng-Content Example Tutorial

Read Now →