处理来自服务器的null响应

时间:2017-07-25 14:22:09

标签: angular

我有一个ngIf条件来检查响应数据。当没有数据时,我得到一个空响应。如果它为null,我想显示“No data Found”。我试过了ngIf="!addresses"但它不起作用我该如何处理呢? 模板:

<div class="col-md-9 col-sm-9 col-xs-9 *ngIf="(!addresses )">  

              <div class="col-md-9 col-sm-9 col-xs-9"> 
               No Data Found  

            </div>
          </div>

组件:

getData(id){

        this.service.GetData(id)
        .subscribe(addresses => this.addresses = addresses)
        this.OnceHttp = !this.OnceHttp;
        }

回应: Null 服务:

GetData(id){
                const eurl='http://localhost/Angular/GetData.php?id='
                const url= `${eurl}${id}`
                return this.http.get(url)
                .map(this.extractData)
                .catch(this.HandleError)

            }

我从服务器获取空值。谁能告诉我,我做错了什么?

2 个答案:

答案 0 :(得分:0)

更新

您可以在组件中定义empty属性,如下所示:

getAddress(id){
    this.dataservice.Getaddress(id)
    .subscribe(addresses => this.addresses = addresses);
    this.OnceHttp = !this.OnceHttp;
    }

get empty(){
   return this.addresses === null || this.addresses.length === 0;
}

然后您可以将模板中的get属性用作

<div *ngIf="empty">//code to show when addresses has no content</div>

答案 1 :(得分:0)

从最外层的div中删除hidden

<div class="col-md-9 col-sm-9 col-xs-9 hidden margin-top" id="blank-skills" 
     *ngIf="!addresses">  

我会做一个空检查,而不是!addresses检查地址== null,但这只是我的观点:)

如果没有hidden,它应该有效:http://plnkr.co/edit/3bKTBjkiQHdBZmnqyhRi?p=preview