动态标记最大值

时间:2018-04-07 13:34:02

标签: javascript html angular typescript

我有一个带有每个播放器的统计对象的数组。我在列表中显示每个玩家的价值:

<tr>
    <td *ngFor="let stat of stats">
        <div > {{stat.gameCount}}</div>
    </td>
</tr>
<tr>
    <td *ngFor="let stat of stats">
        <div > {{stat.winCount}}</div>
    </td>
</tr>

现在的问题是,我想强调每个统计数据的最佳播放器。因此,在第一行中,我想给Div提供另一个类的最大游戏数。在第二行中与最大赢数相同。

我已经知道,我如何动态地将一个类应用于td,但不知道如何检查它是否为最大值。

我认为我可以通过给出第一个td来实现它,例如此属性,但 TypeScript 只给我一个无用的错误消息:

[[class.stat_lbl_value_best]]="Math.max(stats.map(function(value){return value.gameCount})==stat.gameCount";

TypeScript错误是:

  

ng:TypeError:无法读取未定义的属性toUpperCase。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这种方法几乎是正确的,但您需要在typescript类而不是模板中移动代码。 因此,例如,您可以创建一个保持最大值的新属性:

maxWins = Math.max(...this.stats.map(s => s.winCount));

为了有条件地向一个元素添加一个类,可以使用NgClass指令:

<td *ngFor="let stat of stats"
    [ngClass]="{'stat_lbl_value_best': stat.winCount === maxWins}">

您可以以类似的方式实现游戏计数的功能。