【发布时间】:2018-11-02 15:14:50
【问题描述】:
我正在寻找一种使用 *ngFor 生成列表的方法,其中每个生成的列表将具有 3 种可能的背景颜色中的 1 种。我的 .ts 文件中有以下内容:
this.items = [
{id:'00001', status:'pending', title: 'hi1', description: 'test1'},
{id:'00002', status:'pending', title: 'hi2', description: 'test2'},
{id:'00003', status:'pending', title: 'hi3', description: 'test3'}
];
我的 .html 文件中有这个:
<ion-content>
<ion-list>
<ion-item *ngFor="let item of items" (click)="viewItem(item)" ng-style="{'background-color': {{item.status}}}">{{item.title}}</ion-item>
</ion-list>
</ion-content>
然后我在 variables.svss 文件中添加了 $colors。该文件现在看起来像这样:
$colors: (
primary: #488aff,
secondary: #32db64,
danger: #f53d3d,
light: #f4f4f4,
dark: #222,
pending: #FFFF99,
complete: #9966ff,
overdue: #ff0000
);
所以,我的目标是让 ion-item 使用它的 this.status 来确定正确的背景颜色。当我运行我的应用程序时,这是生成的 html:
<ion-item class="item item-block item-ios" ng-style="{'background-color': item.status}">
显然,我不打算将“item.status”作为背景色,我希望使用#FFFF99。
非常感谢任何建议或意见。
【问题讨论】:
-
这会很困难,因为
scss是一种语言,需要在运行应用程序之前编译成css。你想在运行时选择你的颜色。您可以在 Javascript 对象中定义颜色并以这种方式选择它