Angular获取项目价值NGFOR

发布于 2025-01-18 15:50:51 字数 1514 浏览 5 评论 0原文

目前正在迭代 cart.items。每个项目都有所有者的用户名,我想显示所有者的用户名和我购买的产品。问题是我只能迭代 cart.items 而不是所有者的用户名,所以我得到了重复的购物车项目。

我实际上将 item 从父 ngFor 传递给子,这样我就可以获得 items.username。

父 html:

<ng-container *ngIf="cart.items.length">
    <div *ngFor="let item of cart.items">
        <chlid-component [item]="item"></child-component>
    </div>
</ng-container>

我从 cartService 获取 cart.items。

child-component.component:

@Input() item: ICartItem;
username: string;

getUser() {
    const user = this.userService.getUser(this.item.user).subscribe(
      (user: IUser) => {
        this.username = user.username;
        this.userImage = user.image;
      }
    )
  }

chlid-component.html:

<ng-container *ngIf="(cart$ | async) as cart">
    <div class="avatar">
        <div class="name" routerLink="/users/{{username}}">{{username}}</div>
    </div>
<ng-container *ngFor="let item of cart.items">
    <div class="item" *ngIf="item.user == username">
        // some code here
    </div>               
</ng-container>

我从父级的项目中获取用户名,以列出我从所有者那里购买的项目。但存在一个问题,我可以从所有者那里购买多个物品,因此它会显示所有者的物品两次。即

User-1:    User-1:   User-2:
item1,     item1,    item3
item2,     item2

在子组件中,我尝试将 ngFor 值传递给哈希集并检查该用户名是否已经存在。我如何获取 item.username 值并将其传递给 hashSet 以及是否有正确的方法来实现这一点。我检查了 console.log,只得到了 3 个项目的数百条响应。您能提出实现这一目标的正确方法吗?

Currently im iterating through cart.items. Each item has owner's username and i want to display the owner's username and his products that i have purchased. Problem is i can only iterate through cart.items and not by owner's username so im getting duplicate cart items.

Im actually passing item from parent ngFor to child so i can get items.username.

Parent html:

<ng-container *ngIf="cart.items.length">
    <div *ngFor="let item of cart.items">
        <chlid-component [item]="item"></child-component>
    </div>
</ng-container>

Im getting cart.items from my cartService.

child-component.component:

@Input() item: ICartItem;
username: string;

getUser() {
    const user = this.userService.getUser(this.item.user).subscribe(
      (user: IUser) => {
        this.username = user.username;
        this.userImage = user.image;
      }
    )
  }

chlid-component.html:

<ng-container *ngIf="(cart$ | async) as cart">
    <div class="avatar">
        <div class="name" routerLink="/users/{{username}}">{{username}}</div>
    </div>
<ng-container *ngFor="let item of cart.items">
    <div class="item" *ngIf="item.user == username">
        // some code here
    </div>               
</ng-container>

im getting username from parent's item to list item's that i bought from owner. But there is problem that i can buy multiple items from owner so it displays owner's items twice. i.e

User-1:    User-1:   User-2:
item1,     item1,    item3
item2,     item2

In child-component i'm trying to pass ngFor value to hashset and check if that username already exit's in there. How can i get item.username value and pass it to hashSet and is there a proper way to achieve that. I checked console.log and got hundreds of response for just 3 items. Can you suggest a proper way to achieve that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-01-25 15:50:51

好的,要根据您的需求运行它,我必须按用户名分组这些项目,而它们使用了ngfor上的KeyValue管道,您可以在此处阅读 https://angular.io/api/common/common/keyvaluepipe

这是更新的 stackblitz

分组项目

 this.cart.items.forEach((item) => {
  if (!this.groupedItems[item.username]) {
    this.groupedItems[item.username] = [];
  }
  this.groupedItems[item.username].push(item);
});

和显示

<div *ngFor="let group of groupedItems | keyvalue">
  <div class="avatar">
    <div class="name">{{ group.key }}</div>
  </div>

  <div *ngFor="let item of group.value">
    <app-child [item]="item"></app-child>
  </div>
</div>

Ok, to make it running according to your needs I had to group the items by username and them I used the KeyValue pipe on the ngFor, you can read about it here https://angular.io/api/common/KeyValuePipe

here is the updated stackblitz

to group the items

 this.cart.items.forEach((item) => {
  if (!this.groupedItems[item.username]) {
    this.groupedItems[item.username] = [];
  }
  this.groupedItems[item.username].push(item);
});

and the display

<div *ngFor="let group of groupedItems | keyvalue">
  <div class="avatar">
    <div class="name">{{ group.key }}</div>
  </div>

  <div *ngFor="let item of group.value">
    <app-child [item]="item"></app-child>
  </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文