【问题标题】:async pipe as not working within an input in Angular异步管道在 Angular 的输入中不起作用
【发布时间】:2018-06-28 11:16:12
【问题描述】:

我习惯于在 Angular HTML 模板中使用带有“as”的异步管道来避免复制可观察订阅,如下所示:

<component *ngIf="(selected$ | async) as selected"></component>

所以我可以在模板中的其他任何地方使用“selected”。

但是如果我尝试像这样使用它,在输入中:

<component [param]="(selected$ | async) as selected"></component>

我收到一个错误:

Unexpected token 'as' at column 21 in [categories$ | async as categories]

知道为什么吗? 谢谢!

【问题讨论】:

    标签: angular rxjs


    【解决方案1】:

    正如马丁所说,as 语法特定于*ngIf。但是你可以使用ng-container 来获得你想要的结果:

    <ng-container *ngIf="(selected$ | async) as selected">
        <component [param]="selected"></component>
    </ng-container>
    

    【讨论】:

      【解决方案2】:

      没错,as 语法特定于*ngIf。它不是您可以在 Angular 模板中的任何位置使用的通用关键字。

      查看https://angular.io/api/common/NgIf 并搜索NgIfAs 类。

      【讨论】:

      • 如何使用异步变量来禁用按钮。我们需要使用函数吗?
      猜你喜欢
      • 2016-07-25
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-26
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      相关资源
      最近更新 更多