【问题标题】:Cannot read property 'toLowerCase' of undefined Angular 5无法读取未定义 Angular 5 的属性“toLowerCase”
【发布时间】:2018-08-07 11:54:06
【问题描述】:

我正在尝试过滤 ngx 表的列,我按照示例进行操作,但它不断给出错误“无法读取未定义的属性 'toLowerCase'”

这是模板部分

<ngx-datatable-column name="Nom" class="name">
  <ng-template ngx-datatable-cell-template let-value="value">
    {{value}}
  </ng-template>
</ngx-datatable-column>

及其附带的功能

updateFilter(event) {
const val = event.target.value.toLowerCase();

// filter the data
const temp = this.temp.filter(function(d) {
  return d.name.toLowerCase().indexOf(val) !== -1 || !val;
});

// update the rows
this.rows = temp;
// Whenever the filter changes, always go back to the first page
this.table.offset = 0;}

知道如何解决这个问题吗?

【问题讨论】:

标签: javascript angular


【解决方案1】:

似乎被过滤的项目并不都包含值“名称”。在您返回该函数之前尝试console.log(d),以验证您是否收到了预期的数据。

【讨论】:

    【解决方案2】:

    在某些情况下,您的 name 属性似乎必须未定义。这可能是它不是正确的属性名称(它是lastName 而不是name?)或者对于temp 数组中的某些对象,该值未定义。无论哪种情况,您都可以在返回值之前先检查是否为空。

    if (d.name) {
      return d.name.toLowerCase().indexOf(val) !== -1 || !val;
    } else {
      return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-30
      • 1970-01-01
      • 2021-11-28
      • 2018-08-29
      • 2021-08-01
      • 2018-06-01
      • 2020-07-23
      • 2020-06-24
      • 2018-10-23
      相关资源
      最近更新 更多