使用Angular 13处理后的可观察到可观察的请求

发布于 2025-02-06 01:52:05 字数 804 浏览 2 评论 0原文

我正在尝试提出获取数据的请求,然后填充< ul>。当前,以下代码可以正常显示,但我正在尝试使用异步管,因为它将自动处理订阅和取消订阅(据我了解该主题):

  this.dataService.getAllCompetitions()
  .subscribe(competition=> 
    {
      this.competitions = competition;
    }); 

然后是模板:

<div class="competitions-table">
   <ul *ngFor="let comp of competitions?.data">{{comp.name}}</ul>
</div>

现在我尝试实现实现时通过删除初始订阅并分配可观察到的this.competitions变量

this.competitions = this.dataService.getAllCompetitions();

,然后我相应地更改模板:

<div class="competitions-table">
    <ul *ngFor="let comp of competitions?.data | async">{{comp.name}}</ul>
</div>

在模板中没有显示数据,所以我不确定我去哪里了错误或我目前对实施不了解的内容。

I am trying to do a get request to get data and then populate a <ul>. Currently the following code works and the data is displayed correctly but I am trying to use the async pipe as it will handle the subscribing and unsubscribing automatically (as far as I understand the topic):

  this.dataService.getAllCompetitions()
  .subscribe(competition=> 
    {
      this.competitions = competition;
    }); 

And then the template:

<div class="competitions-table">
   <ul *ngFor="let comp of competitions?.data">{{comp.name}}</ul>
</div>

Now when I try to implement the async pipe by removing the initial subscribe and assigning the observable to the this.competitions variable

this.competitions = this.dataService.getAllCompetitions();

And then I changed the template accordingly:

<div class="competitions-table">
    <ul *ngFor="let comp of competitions?.data | async">{{comp.name}}</ul>
</div>

No data is displayed in the template so I'm not quite sure where i went wrong or what I currently don't understand about the implementation.

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

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

发布评论

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

评论(1

长亭外,古道边 2025-02-13 01:52:06

由于data是从getallCompetitions的可观察到的对象的一部分,因此,您需要 map 首先使用它,如以下内容:

this.competitions = this.dataService
  .getAllCompetitions()
  .pipe(map((res) => res.data));

然后,您可以在组件模板中使用它:

<div class="competitions-table">
  <ul *ngFor="let comp of competitions | async">{{ comp.name }}</ul>
</div>

或者,您可以访问>数据直接在组件模板中,如下:

<div class="competitions-table">
  <ul *ngFor="let comp of (competitions | async)?.data">{{ comp.name }}</ul>
</div>

Since the data is a part of the object returned from the getAllCompetitions's Observable`, then, you need to map it first before using it, like the following:

this.competitions = this.dataService
  .getAllCompetitions()
  .pipe(map((res) => res.data));

Then, you can use it in the component template like the following:

<div class="competitions-table">
  <ul *ngFor="let comp of competitions | async">{{ comp.name }}</ul>
</div>

OR, you can access data directly in the component template, like the following:

<div class="competitions-table">
  <ul *ngFor="let comp of (competitions | async)?.data">{{ comp.name }}</ul>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文