【问题标题】:<ION-ITEM> with background color<ION-ITEM> 带背景色
【发布时间】: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 对象中定义颜色并以这种方式选择它

标签: cordova ionic-framework


【解决方案1】:

为什么不为可能的状态添加一些 (S)CSS 类,如下所示:

.pending {
     background-color: #FFFF99;
}
.complete {
     background-color: #9966ff;
}
.overdue {
     background-color: #ff0000;
}

然后根据物品的状态动态设置物品的类别,如下所示:

<ion-item *ngFor="let item of items" [class]="item.status">{{item.title}}</ion-item>

【讨论】:

  • @user9088454 究竟是什么不工作?也许您可以创建一个 MVCE 并使用您的代码创建另一个问题...
猜你喜欢
  • 2019-08-01
  • 2019-06-03
  • 2015-10-24
  • 2018-02-08
  • 2018-09-25
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
相关资源
最近更新 更多