app.component.ts
import {Component} from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { appStatus = new Promise((resolve, reject) => { setTimeout(() => { resolve('stable'); }, 2000); }); servers = [ { instanceType: 'medium', name: 'Production Server', status: 'stable', started: new Date(15, 1, 2017) }, { instanceType: 'large', name: 'User Database', status: 'stable', started: new Date(15, 1, 2017) }, { instanceType: 'small', name: 'Development Server', status: 'offline', started: new Date(15, 1, 2017) }, { instanceType: 'small', name: 'Testing Environment Server', status: 'stable', started: new Date(15, 1, 2017) } ]; getStatusClasses(server: { instanceType: string, name: string, status: string, started: Date }) { return { 'list-group-item-success': server.status === 'stable', 'list-group-item-warning': server.status === 'offline', 'list-group-item-danger': server.status === 'critical' }; } }
app.component.html
<div class="container"> <div class="row"> <div class="col-xs-12 col-sm-10 col-md-8 col-sm-offset-1 col-md-offset-2"> <h3>App Status : {{appStatus|async}}</h3> <ul class="list-group"> <li class="list-group-item" *ngFor="let server of servers" [ngClass]="getStatusClasses(server)"> <span class="badge"> {{ server.status }} </span> <strong>{{ server.name | shorten:5}}</strong> | {{ server.instanceType | uppercase }} | {{ server.started | date:'fullDate' | uppercase }} </li> </ul> </div> </div> </div>
References
https://angular.io/api/common/AsyncPipe