如何使用agular中的ngclass在行的每一列上显示不同的颜色?
我有4行,每行包含3张卡。 我正在从DB获取这些卡的信息,然后在HTML中使用NGFOR渲染它们。 问题是我希望一排的每条卡都显示出不同的颜色。
示例: - >
第1行: 蓝卡| Whitecard | Redcard
Row 2: 蓝卡| Whitecard |雷德卡(Redcard)
到目前为止,我能够使用两种颜色做,但是我该如何制作3种颜色的颜色。
<div *ngFor="let r of rates; let i=index;">
<div class="box" [ngClass]="i % 2 == 0 ? 'BLUECARD' : 'REDCARD'">
<!-- card contents -->
</div>
</div>
如您所见,我可以反而显示2种颜色,但是如何显示3种颜色?
I have 4 rows, each row contains 3 cards.
I am fetching information for these cards from db then rendering them using ngfor in html.
The problem is I want each of card of a row to show different color.
Example:->
ROW 1:
BLUECARD | WHITECARD | REDCARD
ROW 2:
BLUECARD | WHITECARD | REDCARD
as of now I able to do for two colors but how can I make it for 3 colors as I stated in example.
<div *ngFor="let r of rates; let i=index;">
<div class="box" [ngClass]="i % 2 == 0 ? 'BLUECARD' : 'REDCARD'">
<!-- card contents -->
</div>
</div>
As you can see I am able to show 2 colors alternatively but how can I show 3 colors instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用您的颜色创建一个字符串数组,然后
[ngclass] = colorArray [i]
或类似的内容。I would create an string array with your colors and then
[ngClass]=colorArray[i]
or something similar.您可以尝试这些(同时我只使用愚蠢的逻辑): -
或
you can try these (meanwhile i am using silly logic for example only) :-
or
我不得不做这样的事情
我创建了一个函数,该函数根据您作为参数提供的颜色返回颜色;
颜色,
在您的情况下,您可以创建一个函数,该函数将i作为参数并返回ts文件中的
可以使用ngclass尝试
I had to do something like this
I created a function that returns a color based on what you give it as a parameter;
In your case, you can create a function that takes i as parameter and returns a color
In your ts file
you can try with ngClass