首页 > 解决方案 > 基于对象字段值 Angular 6 的颜色 td

问题描述

我有一个表格,我想根据一个属性为这样的行着色:如果account相同,则用灰色着色,如果不一样,用蓝色着色。这是我的代码:

func() {
    for (let i = 0; i < this.List.length; i++) {
      if (this.List[i].account == this.List[i + 1].account) {
        this.List[i].color = "#f2f3f4"
      } else {
        if (this.List[i].account != this.List[i + 1].account && this.List[i].color != "#f2f3f4") {
          this.List[i].color = "rgba(173, 216, 230, 0.35)"
        }
      }
    }
  }

但它不能正常工作。如何修改代码?这是一个有效的闪电战

我也收到此错误:(我猜它来自List[i + 1]

ERROR 错误:无法读取未定义的属性“帐户”

标签: javascriptangularangular6

解决方案


基本索引错误。

您正在循环整个数组(从0this.List.length),然后尝试访问this.List[i + 1].

循环从0this.List.length - 1

for (let i = 0; i < this.List.length - 1; i++) {

推荐阅读