亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以角度迭代對象數組

如何以角度迭代對象數組

料青山看我應如是 2023-09-04 15:45:05
我有一個 BookArr 類書籍對象數組。book.ts 類export class Book{bookId:number;bookName:string;cost:number;quantity:number;constructor(bookId, bookType, cost, quantity){    this.cost=cost;    this.bookId=bookId;    this.bookName=bookName;    this.quantity=quantity;}}booksArr在 books-list.component.ts 中   booksArr: book[] = [    new book(100, "The Alchemist", 20, 1),    new book(101, "Rich Dad Poor Dad", 50, 2),    new book(102, "Charolett's Web", 10, 1),    new book(103, "Harry Potter", 70, 4),    new book(104, "Gone Girl", 150, 3),];我想用html創建一個表格來顯示這些書籍的詳細信息。 books-list.component.html<table border="1" *ngIf="bName">    <thead>      <tr>        <th>book ID</th>        <th>book Name</th>      </tr>    </thead>    <tbody>      <tr *ngFor="let b of booksArr">        <td *ngIf="b.//WHAT SHOULD I PUT HERE"</td>      </tr>    </tbody>  </table>
查看完整描述

4 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

您已經在迭代 booksArr。只需要使用插值來顯示數據。嘗試這樣


<table border="1" *ngIf="bName">

    <thead>

        <tr>

            <th>book ID</th>

            <th>book Name</th>

        </tr>

    </thead>

    <tbody>

        <tr *ngFor="let b of booksArr">

            <td>{{b.bookId}}</td>

            <td>{{b.bookName}}</td>

        </tr>

    </tbody>

</table>


查看完整回答
反對 回復 2023-09-04
?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

您已經在使用 迭代書籍數組*ngFor="let b of booksArr"。


您現在想要對數組中每本書的值進行插值。當您位于數組內部時,您可以訪問在 中聲明的循環變量*ngFor。在這種情況下,循環變量是b。b您現在可以綁定到使用大括號插值的屬性。{{b.bookId}}。


<table border="1" *ngIf="bName">

  <thead>

    <tr>

      <th>book ID</th>

      <th>book Name</th>

    </tr>

  </thead>

  <tbody>

    <tr *ngFor="let b of booksArr">

      <td>{{b.bookId}}</td>

      <td>{{b.bookName}}</td>

    </tr>

  </tbody>

</table>


查看完整回答
反對 回復 2023-09-04
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

在這種情況下不需要*ngIf。只需嘗試以下操作


<tbody>

        <tr *ngFor="let b of booksArr">

            <td>{{b.bookId}}</td>

            <td>{{b.bookName}}</td>

        </tr>

</tbody>

在你的 .ts 文件中你應該改變


constructor(bookId, bookName, cost, quantity)

或者


this.bookName=bookType;

在你的構造函數中


查看完整回答
反對 回復 2023-09-04
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

嘗試:

<td> {{ b.bookId }} </td>
<td> {{ b.bookName }} </td>


查看完整回答
反對 回復 2023-09-04
  • 4 回答
  • 0 關注
  • 185 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號