Angular 如何检查places变量不为null?
我尝试使用角度材质自动完成,但我有一个错误。错误是Angular 如何检查places变量不为null?,angular,typescript,Angular,Typescript,我尝试使用角度材质自动完成,但我有一个错误。错误是 : ERROR TypeError: Cannot read property 'filter' of undefined 我认为,问题在于当应用过滤器时,place[]为空。我不知道如何解决这个问题 数据进入PHP后端 This is my TS file: https://pastebin.com/cAA3ycAA And this is my template: https://pastebin.com/cxBnr5WU 谢谢你的帮
: ERROR TypeError: Cannot read property 'filter' of undefined
我认为,问题在于当应用过滤器时,place[]
为空。我不知道如何解决这个问题
数据进入PHP后端
This is my TS file: https://pastebin.com/cAA3ycAA
And this is my template: https://pastebin.com/cxBnr5WU
谢谢你的帮助。
因此,我尝试检查长度,但它导致了一些错误,然后我尝试检查位置是否为非null或未定义,但这是导致filter()上出现相同错误的原因,如下所示:
请仔细检查您的代码,因为我可以看到您在一条语句中使用了
places
,然后在另一条语句中使用place
,在该语句中您声明了this.place.valueChanges
请检查代码的这一部分
{{option.name}
首先,只需将option.name替换为option
然后在这段代码中
private_过滤器(值:字符串):Place[]{
常量filterValue=value.toLowerCase();
返回this.places.filter(places=>places.name.toLowerCase().includes(filterValue));
}
A.尝试用字符串[]替换位置[]
B.代替.includes(filterValue),请尝试
返回此.places.filter(places=>places.name.toLowerCase().indexOf(value.toLowerCase())==0)
如果有帮助,请告诉我。谢谢:)有两种简单的可能性: 1) 返回前检查是否为null,否则返回空数组:
return this.places ? this.places.filter(places => places.name.toLowerCase().includes(filterValue)) : [];
places:Place[] = [];
2) 默认情况下,将位置设置为空数组:
return this.places ? this.places.filter(places => places.name.toLowerCase().includes(filterValue)) : [];
places:Place[] = [];
@bandy add检查
位置的长度
如果其大于0
则应用过滤器。