gpt4 book ai didi

angular - 有没有办法将管道的结果保存在 Angular 2 的模板输入变量中?

转载 作者:太空狗 更新时间:2023-10-29 17:55:15 25 4
gpt4 key购买 nike

我真的很喜欢 *ngFor 指令的语法。您可以遍历数组的所有元素。

我想用管道的结果实现同样的事情。例如 async 管道。将 async 管道放置在我使用可观察结果的属性的模板中的任何位置时,我有很多订阅正在进行并且我的模板被诸如 {{( someObservable$ | async)?.propertyName}}。有没有办法像这样简单地将管道的当前结果绑定(bind)到模板输入变量?

<div let="currentValue = someObservable$ | async">
{{currentValue?.foo}}, {{currentValue?.bar}}
...
</div>

我尝试将它与 *ngIf 结合使用,但这似乎不是一个有效的模板表达式:

<div *ngIf="let currentValue = someObservable$ | async">
{{currentValue?.foo}}, {{currentValue?.bar}}
...
</div>

最佳答案

是的!从 Angular 4+ 开始,我们可以存储成功 *ngIf 的结果,例如:

<div *ngIf="userObservable | async; else loading; let user">
Hello {{user.last}}, {{user.first}}!
</div>
<template #loading>Waiting...</template>

If you need same with *ngFor, simply wrap it in *ngIf ;-)

Source

关于angular - 有没有办法将管道的结果保存在 Angular 2 的模板输入变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37882333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com