Angular,TypeError:无法读取未定义

时间:2017-11-17 09:04:40

标签: html angular typescript

我正在尝试按照此link制作自定义过滤器管道,但我收到错误Angular, TypeError: Cannot read property 'toLowerCase' of undefined。我已经将管道导入app.module.ts并在declaration中声明了它。任何人都可以帮我解决这个错误吗?

<form id="filter">
  <input type="text" class="form-control" name="term" [(ngModel)]="term" placeholder="filter by name" />
</form>

<tr *ngFor="let product of productList | paginate: { itemsPerPage: 1, currentPage: p } | filter: term">
  <td>{{product.prdName}}</td>
  <td>{{product.prdCat}}</td>
  <td>{{product.prdSup}}</td>
</tr>

@Pipe({
  name: 'filter'
})


transform(prdName: any, term: any): any {
    if (term === undefined) return prdName;

    return prdName.filter(function(Product) {
      return Product.name.toLowerCase().includes(term.toLowerCase());
    })

2 个答案:

答案 0 :(得分:2)

尝试这样:

transform(items: any, term: any): any {
    if (term === undefined) return items;

    return items.filter(function(Product) {
        return Product.prdName.toLowerCase().includes(term.toLowerCase());
    })
}

答案 1 :(得分:1)

名为'name'或'prdName'的属性(您在模板中使用的属性是'prdName')?

transform(values, args) {
    values.filter(
      product => product.prdName.toLowerCase().includes(args.toLowerCase())
    )
}