无法读取角度2中null的属性"filter"? [英] Cannot read property 'filter' of null in angular 2?
本文介绍了无法读取角度2中null的属性"filter"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此错误无法读取null的属性过滤器"
当我在角度2中应用滤镜时,这是我的代码
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;
}
}
推荐答案
这是因为您具有作为输入的数据,这些数据是使用HTTP请求异步加载的.
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;
}
}
这篇关于无法读取角度2中null的属性"filter"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文