簡體   English   中英

Angular 7 ngFor 迭代對象數組

[英]Angular 7 ngFor iterate over array of objects

我無法迭代對象數組。 在每個對象中,我都有一個名稱,但是我無法獲取對象的文本或其中的名稱。

我的 ts 文件中有以下代碼

retrieveMultiple(config, "accounts")
.then(
    (results) => {
        const accounts: any[] = [];
        for (let record of results.value) {
            accounts.push(record);
        }

        this.accounts= accounts;
        console.log(accounts[0].name);
        console.log(accounts);
    },
    (error) => {
        console.log(error);
    }

我的 HTML 文件中有這個:

      <p>Accounts:</p>
  <ul>
    <li *ngFor="let account of accounts | json">
      {{ account.name }}
    </li>
  </ul>

您的循環表達式中不應包含json管道。 將您的輸出代碼更改為:

<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account.name }}
  </li>
</ul>

或者為了調試,你可以使用這個:

<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account | json }}
  </li>
</ul>

https://angular.io/api/common/NgForOf

retrieveMultiple(config, "accounts")
.then(
    (results) => {
        this.accounts = results.value;
        console.log(this.accounts[0].name);
        console.log(this.accounts);
    },
    (error) => {
        console.log(error);
    }
)
<p>Accounts:</p>
<ul>
  <li *ngFor="let account of accounts">
    {{ account.name }}
  </li>
</ul>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM