【发布时间】:2019-04-11 13:42:36
【问题描述】:
我在这里有一个 mixin 变量:
component.scss
$bg-selected-list-item: #1E62F1;
$bg-list-item: #FFF;
如何在[ngStyle] 中使用上面的 mixin 变量:
component.html
<mat-list-item *ngFor="let OBJ of dataSOURCE" [ngStyle]="{'background-color': OBJ.is_selected ? $bg-selected-list-item : (OBJ.back_color || $bg-list-item) }">
</mat-list-item>
期待
案例1(如果选择了ListItem):那么使用mixin变量$bg-selected-list-item作为列表项的背景色
案例 2(如果未选择 ListItem 并且如果 OBJ 设置了 back_color)
->然后使用OBJ.back_color作为列表项的背景色
案例 3(如果未选择 ListItem 并且如果 OBJ 设置了 NO back_color)
->然后使用mixin变量$bg-list-item作为列表项的背景色。
我面临以下问题
这两个 Mixin 变量颜色都没有应用于 ngStyle**
仅应用 OBJ.back_color**。
下面是答案(感谢@matirmv)
component.scss
$bg-selected-list-item: #1E62F1;
$bg-list-item: #FFF;
.my-background-class{
background-color:$bg-list-item;
}
.my-background-selected-class{
background-color:$bg-selected-list-item;
}
component.html
<mat-list-item *ngFor="let OBJ of dataSOURCE" class="my-background-class" [ngStyle]="!OBJ.is_selected && {'background-color': OBJ.back_color}"
[ngClass]="{'my-background-selected-class': OBJ.is_selected}">
【问题讨论】: