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
    ],
    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 = 'ngClass Example' ;
    cssStringVar: string= 'red size20';
    cssClass: CssClass = new CssClass();
}
class CssClass {
    red: boolean= true;
    size20: boolean= true;
}

3)app.component.html

<p> {{title}} </p>
<div class='panel panel-primary'>
    <div class='panel-heading'>
        <p>Simple example of ngClass </p> 
    </div>
   <div class="panel-body">
        <div class="row">
            <div [ngClass]="'red size20'">
                Red Text with Size 20px  : as string
            </div>
        </div>
         <!-- This also works.••• -->
        <div class="row">
            <div ngClass='red size20'>
                Red Text with Size 20px  : as string
            </div>
        </div>
        <div class="row">
            <div [ngClass]="['red','size20']">
                Red Text with Size 20px  : as array
            </div>
       </div>
        <div class="row">
            <div [ngClass]="{'red':true,'size20':true}">
                Red Text with Size 20px  : as object
            </div>
       </div>
        <!-- Getting Data from Component. You can modify the CSS From the component••• -->
        <div class="row">
            <div [ngClass]="cssStringVar">
                Red Text with Size 20px  : from component
            </div>
        </div>
        <div class="row">
            <div [ngClass]="cssClass">
                Red Text with Size 20px  : from component as object
            </div>
       </div>

    </div>
</div>

results matching ""

    No results matching ""