-1

我有一个要显示的值集。这是它的结构。我遇到的问题是尝试显示第三项,因为它的结构略有不同。请注意,这可以是动态的,因此它并不总是具有不同结构的第三项。

[
  {
    "id": "1b255d6d-f771-4c16-9d64-fdb188dc9298",
    "categoryId": null,
    "filename": "dummy.pdf",
    "url": "http..."
  },
  {
    "id": "8d17ee10-54e0-48eb-8c21-83e7c6ca6751",
    "categoryId": null,
    "filename": "dummy (1).pdf",
    "url": "http..."
  },
  [
    {
      "id": "42b30baf-a945-412e-a53d-803e57c684cf",
      "categoryId": null,
      "filename": "1 Rules of the Road draft1.pdf",
      "url": "http..."
    }
  ],
  {
    "id": "1d0531e0-f0ad-4026-bfa0-5f19fa4fbca0",
    "categoryId": null,
    "filename": "offer.pdf",
    "url": "http..."
  }
]

这是我的 HTML

<div *ngIf="candidateDocuments" class="ui-g-12 ui-md-12 ">
  <div *ngFor="let doc of candidateDocuments" class="ui-g-6 ui-sm-12">
    <div class="doc-container">
      <span
        ><img src="img"
      /></span>
      <span>{{ doc.filename }}{{ doc.filetype }}</span> 
      <button (click)="openDocWindow(doc.url)">...</button>
    </div>
  </div>
</div>

我尝试了以下方法,但它不起作用

<div *ngIf="candidateDocuments" class="ui-g-12 ui-md-12 ">
  <div *ngFor="let doc of candidateDocuments; let i = index" class="ui-g-6 ui-sm-12">
    <div class="doc-container">
      <span
        ><img src="img"
      /></span>
      <span>{{ doc.filename }}{{ doc.filetype }}</span> 
      <button (click)="openDocWindow(doc.url)">...</button>
      <div *ngFor="let userdoc of doc.Array" class="ui-g-6 ui-sm-12">
          <div class="doc-container">
            <span
              ><img src="img"
            /></span>
            <span>{{ userdoc.filename }}{{ userdoc.filetype }}</span> 
            <button (click)="openDocWindow(userdoc.url)">...</button>
          </div>
        </div>
    </div>
  </div>
</div>
4

2 回答 2

0

通过使用 *ngIf else 条件:

<div *ngIf="candidateDocuments" class="ui-g-12 ui-md-12 ">
<div *ngFor="let doc of candidateDocuments" class="ui-g-6 ui-sm-12">
    <div class="doc-container">
        <div *ngIf="doc.filename; else other">
            <span>{{ doc.filename }}{{ doc.filetype }}</span> 
  <button (click)="openDocWindow(doc.url)">...</button>
   </div>
   <ng-template #other>

        <div *ngFor="let d of doc" class="ui-g-6 ui-sm-12">
     {{d.filename}}
        <button (click)="openDocWindow(d.url)">...</button>
          </div>
   </ng-template>
</div>

于 2019-07-25T07:08:15.680 回答
-1

使用||运算符:

为您创建了一个 stackblitz 工作示例

 <div *ngFor="let doc of candidateDocuments" class="ui-g-6 ui-sm-12">
    <div class="doc-container">
      <span><img src="img"/></span>
      <span>{{ doc.filename || doc[0].filename}}{{ doc.filetype || doc[0].filetype }}</span> 
      <button (click)="openDocWindow(doc.url || doc[0].url)">...</button>
    </div>
  </div>
于 2019-07-25T06:58:05.267 回答