Stack Overflow Asked by mafortis on December 14, 2020
I want to know how can I add dynamic class name to my <div>
based on item values in angular?
item status is open
add class bg-success
Or item status is pending
add class bg-warning
Component
const badgeColors = {
'Open': 'bg-primary',
'pending': 'bg-warning',
'Finished': 'bg-success',
'Expired': 'bg-danger'
}
HTML
<div class="col-md-3" *ngFor="let item of listOfData" :key="item.id">
<div class="card">
<div class="otherClasses bg-warning"></div> <-- I need to change this static `bg-warning` to dynamic `badgeColors`
</div>
</div>
you can simply call ngClass on your div like
[ngClass]="displayDynamicColour(item.status)"
and in your ts
const badgeColors = {
'Open': 'bg-primary',
'pending': 'bg-warning',
'Finished': 'bg-success',
'Expired': 'bg-danger'
}
displayDynamicColour(item){
return this.badgeColors[item];
}
Correct answer by Hrishikesh Kale on December 14, 2020
You can use ngClass directive. For example:
<div [ngClass]="{'bg-success': (status === open), 'bg-warning': (status === pending)}"> </div>
Answered by Utkarsh Awasthi on December 14, 2020
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP