Angular,TypeError:无法读取未定义的属性'toLowerCase' [英] Angular, TypeError: Cannot read property 'toLowerCase' of undefined

查看:1993
本文介绍了Angular,TypeError:无法读取未定义的属性'toLowerCase'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照链接制作自定义过滤器管道,但我得到的错误说 Angular,TypeError:无法读取undefined 的属性'toLowerCase'。我已经将管道导入到 app.module.ts 中,并在声明中声明了它。任何人都可以帮我解决这个错误?



 < form id =filter > < input type =textclass =form-controlname =term[(ngModel)] =termplaceholder =按名称过滤/>< / form>< tr * ngFor = 让productList | paginate:{itemsPerPage:1,currentPage:p} | filter:term> < TD> {{product.prdName}}< / TD> < TD> {{product.prdCat}}< / TD> < td> {{product.prdSup}}< / td>< / tr>  

任何{if(term === undefined)return prdName; return prdName.filter(function(Product){return Product.name.toLowerCase()。includes(term.toLowerCase());})


解决方案

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

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


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.ts and also declared it in declaration. Can anyone help me with this error?

<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());
    })

解决方案

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());
    })
}

这篇关于Angular,TypeError:无法读取未定义的属性'toLowerCase'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆