6

我收到此错误 无法读取 null 的属性“过滤器”

当我在角度 2 中应用过滤器时。这是我的代码

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;
  }
}
4

1 回答 1

14

这是因为您将数据作为输入,使用 HTTP 请求异步加载。

您需要先检查一下,然后才能应用过滤器:

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

    return value.filter((student)=>new RegExp(queryString).test(student.name))
    // return value;
  }
}
于 2016-02-11T10:43:32.743 回答