ItSolutionStuff.com

Angular NgStyle Conditional Example

By Hardik Savani • May 2, 2024
Angular

In this article we will cover on how to implement angular ng style conditional example. it's simple example of ngstyle conditional angular 9. This article goes in detailed on angular 9 ng style conditional. you can understand a concept of ng style with multiple conditions angular 9. follow bellow step for angular ng style conditional example.

In this tutorial, i will show you some example for how to add conditional style css in angular application. you can easily add condition in ngStyle directive in angular application.

i already posted with simple example of ngStyle, you can also read that article Angular ngStyle Example.

You can easily use ngstyle with condition 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 example.

Here, let's see how to use Conditional style with angular application one by one:

Example 1: NgStyle with Condition

src/app/app.component.html

<h1>Angular NgStyle Conditional Example - ItSolutionStuff.com</h1>

<div [ngStyle]="{'color': true ? 'green' : 'red' }">This is condition.</div>

Example 2: NgStyle with condition

src/app/app.component.html

<h1>Angular NgStyle Conditional Example - ItSolutionStuff.com</h1>

<div [ngStyle]="{'color':(country === 'US') ? 'orange' : 'red' }">This is condition.</div>

src/app/app.component.ts

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

@Component({

selector: 'my-app',

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

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

})

export class AppComponent {

name = 'Angular';

country = 'US';

}

I hope it can help you...

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

Angular MultiSelect Dropdown using Ng-select Example

Read Now →

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 NgStyle Example | NgStyle 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-Content Example Tutorial

Read Now →

Angular NgClass - How to Add Dynamic Class in Angular 10/9/8?

Read Now →