typescript Angular,TypeError:无法读取未定义的属性“toLowerCase”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47346800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:02:36  来源:igfitidea点击:

Angular, TypeError: Cannot read property 'toLowerCase' of undefined

htmlangulartypescript

提问by Wira Xie

i'm trying to make a custom filter pipe by following this link, but i got error that said Angular, TypeError: Cannot read property 'toLowerCase' of undefined. I already imported the pipe to app.module.tsand also declared it in declaration. Can anyone help me with this error?

我正在尝试按照此链接制作自定义过滤器管道,但出现错误提示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());
    })

回答by Chandru

try like this :

试试这样:

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

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

回答by Christian Benseler

Is the attribute called 'name' or 'prdName' (the one you used in the template is 'prdName')?

属性是“name”还是“prdName”(您在模板中使用的那个是“prdName”)?

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