如何在html中使用异步
如何在这段代码中为用户包含异步?
<div *ngIf="usersService.loaded$ | async">
<nb-card>
<nb-card-header>
<h1> {{ user?.name }} | {{ user?.age }} </h1>
</nb-card-header>
</nb-card>
使用商店中的方法按如下方式启动用户:
this.user = this.route.params
.pipe(
takeUntil(this.destroyed$),
switchMap(({ userId }) => this.usersService.getSelectedUser(userId))
);
How can I include async here in this code for user?
<div *ngIf="usersService.loaded$ | async">
<nb-card>
<nb-card-header>
<h1> {{ user?.name }} | {{ user?.age }} </h1>
</nb-card-header>
</nb-card>
The user is being initiated as follows using a method from the store:
this.user = this.route.params
.pipe(
takeUntil(this.destroyed$),
switchMap(({ userId }) => this.usersService.getSelectedUser(userId))
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需用方括号将异步管道调用括起来即可:
Just surround the async pipe call with brackets:
这是一种解决方案:
如果您想在卡的其他部分使用用户,可以将
*ngIf="user | async as usr"
移动到;
元素。这样用户就可以在任何卡片内容中使用。在上面的两种情况下,如果用户可观察的值为 null,则 *ngIf 将失败。在某些情况下,当用户为空时,您可能希望使用默认值。我一直在使用这个我可能在某处学到的小技巧:)
实际上,我可能会像这样加入两个可观察量......
Here is one solution:
If you want to use the user in other parts of the card, you can move the
*ngIf="user | async as usr"
to the<nb-card>
element. That way the user would be available in any card content.In the two cases above, if the user observable's value is null, then the *ngIf will fail. In some cases, you may want to use a default value when the user is null. I have been using this little trick that I probably learned somewhere on SO :)
Actually, I would probably join the two observables like this...