Angular NgStyle Conditional Example
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
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Angular MultiSelect Dropdown using Ng-select Example
- How to Get Index of Ngfor in Angular?
- Angular Check ngIf Null or Empty | Angular Check If Array is Empty
- Angular NgForm Example | NgForm Directive In Angular
- Angular NgStyle Example | NgStyle Directive In Angular
- Angular NgSwitch Example | NgSwitch Directive In Angular
- Angular NgIf Example | NgIf Directive In Angular
- Angular NgFor Example | NgFor Directive In Angular
- Angular NgIf Else | Ng If Else in Angular Example
- Angular Ng-Content Example Tutorial
- Angular NgClass - How to Add Dynamic Class in Angular 10/9/8?