angular 2. Pipes. Cannot read property of undefined

隐身守侯 提交于 2019-12-01 22:45:27

Given the template you provided, it looks like instances of Organization have no member called "name", but a member called "organizationName" instead.

Maybe you can try replacing

org.name.toLowerCase().indexOf(filter) != -1

with

org.organizationName.toLowerCase().indexOf(filter) != -1

in your code.

UPDATE

That said, you can try a different way of binding you filter expression:

@Pipe({
  name: 'orgFilter'
})
export class OrgFilterPipe implements PipeTransform {
    transform(orgs: Organization[], expression:string): any {
       if(!expression){
            return orgs;
       }
       else {
            return orgs.filter((org:Organization) => org.organizationName.toLowerCase().indexOf(expression) != -1)
       }
} 

Where expression refers to a member of your component, let's say:

filterExpression: string;

Then change your input text to:

<input type="text" [(ngModel)]="filterExpression">

And change the pipe call to:

<tr *ngFor="#organization of organizations | orgFilter:filterExpression">

According to the documentation for a list filter you may have to declare your pipe as impure using:

@Pipe({
  name: 'orgFilter',
  pure: false

})

It's either going to be the args aren't defined or the org.name isn't defined. You could try debugging it in the DevTools, maybe set the breakpoint at the start of the transform()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!