【发布时间】:2020-05-12 21:07:01
【问题描述】:
我在按钮中使用 ngFor,我从数据中获得了 4 个结果,并且我希望 HTML 中有 4 个按钮。 如何为这个按钮应用 4 种不同的颜色?
附: In ngFor colors I receive obj:
我使用 Object.values 仅从 obj 中获取链接并在 this.img 中分配 colors > 当单击 4 个按钮之一时,我在 HTML 中使用它来显示每个图像。这就是为什么我需要应用到每个按钮的背景颜色
<div class="col-md-4 col-lg-4">
<img [src]="img">
</div>
<div>
<span class="title-description">Select color:</span>
<div class="row" style="margin-left: 0px;">
<button *ngFor="let colors of product.image" class="card ml-1"
(click)="changeColor(colors)"
style="height: 30px; width: 30px;">
</button>
</div>
</div>
changeColor(colors) {
this.img = Object.values(colors);
}
【问题讨论】:
-
如果您查看我的答案中的链接,我认为它可以满足您的需求。