I wanted to apply css class based on the status. Eg: if status is Pass, css class .dot .pass should be assigned to span in html. How I can handle the null value if api return status: null. I'm getting this error message ERROR TypeError: Cannot read property 'css' of undefined. I belived this was caused by null value in status.
status.constant.ts
export const STATUSES_KEY = {
notChecked: 'NOT_CHECKED',
pass: 'PASS',
na: 'NA',
fail: 'FAIL'
};
export const STATUS_STYLING = {
[STATUSES_KEY.notChecked]: {
text: 'Not Checked',
css: 'dot notChecked',
},
[STATUSES_KEY.pass]: {
text: 'Pass',
css: 'dot pass',
},
[STATUSES_KEY.fail]: {
text: 'Fail',
css: 'dot fail',
}
};
app.ts
generateStatusClass(status: string) {
if (status) {
return STATUS_STYLING[status].css;
} else {
return;
}
}
app.html
<span *ngIf="col === 'status'" class="{{ generateStatusClass(element[col]) }}"></span>