Cannot read property 'filter' of null in angular 2?

谁说我不能喝 提交于 2019-12-06 00:36:43

问题


I am getting this error Cannot read property 'filter' of null

when I apply filter in angular 2 .here is my code

http://plnkr.co/edit/K46jJsnmHiONuqIsnuzW?p=preview

import {Pipe} from 'angular2/core';

@Pipe({
  name: 'sortByName',
  pure: false,
})
export class SortByNamePipe {

  transform (value, [queryString]) {
    // console.log(value, queryString);
    return value.filter((student)=>new RegExp(queryString).test(student.name))
    // return value;
  }
}

回答1:


It's because you have data as input that are loaded asynchronously using an HTTP request.

You need to check this before being able to apply the filter:

export class SortByNamePipe {
  transform (value, [queryString]) {
    if (value==null) {
      return null;
    }

    return value.filter((student)=>new RegExp(queryString).test(student.name))
    // return value;
  }
}


来源:https://stackoverflow.com/questions/35337076/cannot-read-property-filter-of-null-in-angular-2

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