使用Angular 13处理后的可观察到可观察的请求
我正在尝试提出获取数据的请求,然后填充< 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
data
是从getallCompetitions
的可观察到的对象的一部分,因此,您需要 map 首先使用它,如以下内容:然后,您可以在组件模板中使用它:
或者,您可以访问
>数据
直接在组件模板中,如下:Since the
data
is a part of the object returned from thegetAllCompetitions
's Observable`, then, you need to map it first before using it, like the following:Then, you can use it in the component template like the following:
OR, you can access
data
directly in the component template, like the following: