我有这个功能
searching() {
this._demoService.postData(this.url,this.searchtobesent).subscribe(
data =>{
this.listings=data;
this.errorMsg="";
if(data){
}
else{
this.errorMsg="Nothing Found";
}
},
error =>{
console.log("error : "+error);
this.errorMsg="Error Loading Your Listings";
}
) ;
}
当我的后端有一些结果(以 Json 格式发送)时,一切正常。 我想要的是当结果为空并且我从服务器收到这个
[]
我想让 this.errorMsg 变成 Nothing Found。 任何帮助,将不胜感激 ;) 谢谢
最佳答案
即使它是一个空数组,它仍然是一个有效的响应。 您可以使用 array.length 来处理这个问题。
if (data && data.length > 0) {
// do something
} else {
this.errorMsg = "Nothing Found";
}
关于json - Angular 2 (TypeScript) 处理来自 Http 的空 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43565893/