Angular NgStyle Example | NgStyle Directive In Angular
Hi Dev,
Here, i will show you angular ng style conditional example. This tutorial will give you simple example of ngstyle with condition angular. you will learn ngstyle with condition in angular. In this article, we will implement a *ngstyle condition in angular example.
You can easily use ng style 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.
*ngStyle allows you to write css on element. In this example i will give you two example one will be simple add css using object and another example of adding css using component function.
Let's see bellow example angular ngStyle directive example.
Example 1: ngStyle with a Object
src/app/app.component.html
<h1>Angular ngStyle Directive Example - ItSolutionstuff.com</h1>
<button [ngStyle]="{background: 'red'}">Click Me!</button>
Example 2: ngStyle with a Function
src/app/app.component.html
<h1>Angular ngStyle Directive Example - ItSolutionstuff.com</h1>
<button [ngStyle]="addButtonStyles()">Click Me!</button>
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 {
addButtonStyles(){
return {
'background': 'blue',
'padding': '12px'
}
}
}
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 NgModel Example | NgModel Directive In Angular
- Angular NgClass Example | NgClass 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?