Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何检查返回的json值是否为空?_Angular - Fatal编程技术网

Angular 如何检查返回的json值是否为空?

Angular 如何检查返回的json值是否为空?,angular,Angular,我想检查json值是否为null。如果info.pic为空,我希望能够显示一个表单来上传图像,否则显示json中返回的图像 我的html <div class="col-sm-9" *ngFor="let info of dInfo"> <img [src]="'data:image/JPG;base64,' + info.pic"/> </div> <h1>name: </h1> <p> {{

我想检查json值是否为null。如果
info.pic
为空,我希望能够显示一个表单来上传图像,否则显示json中返回的图像

我的html

<div class="col-sm-9" *ngFor="let info of dInfo">
  <img [src]="'data:image/JPG;base64,' + info.pic"/>
</div>
      <h1>name: </h1>
      <p> {{ info.name }} </p>
    </div>

 // The upload form that I want to hide 
  <label for="image">Upload</label>
  <input #fileInput type="file" id="image"/>
  <button class="btn" (click)="upload()">Upload Image</button>
接口

export interface DInterface {

   id: number;
   name: string;
   pic: any;
}

JSON响应


使用
*ngIf

就你而言:

<img *ngIf="(info.pic | json) != '{}'" [src]="'data:image/JPG;base64,' + info.pic"/>

更新1:

尝试:

<img *ngIf="info?.pic" [src]="'data:image/JPG;base64,' + info.pic"/>

使用
*ngIf

就你而言:

<img *ngIf="(info.pic | json) != '{}'" [src]="'data:image/JPG;base64,' + info.pic"/>

更新1:

尝试:

<img *ngIf="info?.pic" [src]="'data:image/JPG;base64,' + info.pic"/>


如果我使用
===={}
===NULL
,则此选项无效。它返回一个json对象数组,如图所示。您可以看到它返回的pic为Null。这难道不简单吗:
*ngIf=“info.pic”
?hmmm。。您是否尝试过使用
*ngIf=“info.pic==null”
?@katana314您的解决方案有效。我可以做
*ngIf=“info.pic”
*nfIf=“!info.pic”
@ghan尝试放置
?。
操作符,以防您仍然没有
信息
对象,但您的模板正在渲染。如果我使用
=={}
===NULL
,则此操作无效。它返回一个json对象数组,如图所示。您可以看到它返回的pic为Null。这难道不简单吗:
*ngIf=“info.pic”
?hmmm。。您是否尝试过使用
*ngIf=“info.pic==null”
?@katana314您的解决方案有效。我可以使用
*ngIf=“info.pic”
*nfIf=“!info.pic”
@ghan尝试使用
?。
操作符,以防您仍然没有
信息
对象,但模板正在渲染。