[ngStyle] and [ngClass] Directives
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class ServersComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
//function to get random colors
public getRandomColor(){
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++){
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
}
<p
[ngStyle]="{backgroundColor: getColor()}"
[ngClass]="{online: serverStatus === 'online'}"> {{ 'Server' }} with ID {{ serverId }} is {{ getServerStatus() }} </p>
.online {
color: white;
}