ngStyle: -
1)app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
2)app.component.ts
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app',
templateUrl: './app.component.html',
})
export class AppComponent{
title: string = 'ngStyle Example' ;
size: number = 12;
color: string= 'red';
styleClass: StyleClass = new StyleClass();
}
class StyleClass {
color: string= 'blue';
'font-size.%': number= 150;
'font-weight': string= 'bold';
}
3)app.component.html
<p> {{title}} </p>
<div class='panel panel-primary'>
<div class='panel-heading'>
<p>Simple example of ngIf </p>
</div>
<div class="panel-body">
<input [(ngModel)]="color" />
<div [ngStyle]="{'color': color}">
Change my color
</div>
<input [(ngModel)]="size" />
<div [ngStyle]="{'font-size.px': size}">
Change my size
</div>
<div [ngStyle]="styleClass">
Change my size & Color
</div>
</div>
</div>