javascript - 从 Angular 对象的数据数组填充 View

标签 javascript angular

我在变量 requestData 中有一个数据存储,我想用它来填充通知组件 View

requestData = {
    "bookId": "5e5bb8ac59441513cca2e64c",
    "bookTitle": "Take Great Photographs",
    "request": [
        {
            "_id": "5e21d286b3ee501ebc39d899",
            "firstname": "Ibaji",
            "lastname": "Offor"
        },
        {
            "_id": "5e2c5a20aff8434278639ff5",
            "firstname": "Cheksmate",
            "lastname": "Fidelis"
        }
    ]
}

在 View 中填充上述数据后, View 应该如下图所示

enter image description here

这是 notification.component.html 的代码

<section class="main profile " my-books>
    <div class="profile-header">
      <h1>Notifications</h1>
    </div>

    <div class="book-list-header">
      <h2>Showing Recent Notifications</h2>
      <!-- <span>(Confirm by click the recieved button once book is returned)</span> -->
    </div>

    <div history *ngFor="let item of requestData">
      <a [routerLink]="['details']">
        <h3>9:15 AM | Mon, Feb 11, 2019</h3>
        <div class="first">
          <span>
            <span class="image-cropper" style=" width: 36px; height: 36px;">
              <img src="assets/images/avatar.jpg" alt="profile image" avatar>
            </span>
            <h4>{{item.request.firstname}} {{item.request.lastname}}</h4>
          </span>

          <span>
            <h3>Book Title</h3>
            <h4>{{item.bookTitle}}</h4>
          </span>
        </div>
      </a>
    </div>

</section>

任何帮助的尝试都将非常感激

最佳答案

从上面提到的 requestData 中,数据是一个对象,并且您尝试在无效的对象上使用 *ngFor="let item of requestData"它应该抛出错误。

原因是数组只是 *ngFor 中的可迭代对象。因此,请使用以下内容更改上面的代码,

<div history *ngFor="let item of requestData.request">
  <a [routerLink]="['details']">
    <h3>9:15 AM | Mon, Feb 11, 2019</h3>
    <div class="first">
      <span>
        <span class="image-cropper" style=" width: 36px; height: 36px;">
          <img src="https://www.w3schools.com/howto/img_avatar.png" style="width:50px; border: 2px solid #fff; border-radius:50%" alt="profile image" avatar>
        </span>
        <h4>{{item.firstname}} {{item.lastname}}</h4>
      </span>

      <span>
        <h3>Book Title</h3>
        <h4>{{requestData.bookTitle}}</h4>
      </span>
    </div>
  </a>
</div>

这里 requestData.request 是一个数组,因此您只需使用 *ngFor 迭代此数组并替换 {{item.request.firstname}} {{item.firstname}} 等等所有相关数据。

要分配bookTitle的值,您需要直接从对象中使用,

{{requestData.bookTitle}}

Working stackblitz example here..

关于javascript - 从 Angular 对象的数据数组填充 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60537284/

相关文章:

javascript - 现代网络开发的 cookie 替代品?

javascript - VB.NET smtp.Send 的 jQuery/JavaScript 替代方案

javascript - Intl.NumberFormat#格式化最大整数值

javascript - 单击后悬停不会重置

typescript - 将 Ionic2 popover ngModel 值传递给父页面组件并调用函数?

javascript - 根据选定的传递参数, Angular 隐藏和显示图像

javascript - 将 Selenium 与 Edge Chromium、Javascript 绑定(bind)一起使用时,出现 "WebDriverError: Unknown error"

javascript - 带有动态 ajax url 选项的 ng2-select2

javascript - 迭代 observable.forkJoin 以处理多个数据库的 crud (API)

angular - 让 mat-autocomplete 不替换但增加值(value)