异步管道在 Angular 的输入中不起作用 [英] async pipe as not working within an input in Angular

查看:41
本文介绍了异步管道在 Angular 的输入中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm used to use the async pipe with "as" in an Angular HTML template to avoid replicating observable subscriptions, like this:

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

这样我就可以在模板中的任何其他地方使用selected".

So then I can use "selected" anywhere else in the template.

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

But then if I try to use it like this, in an input:

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

我收到一个错误:

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

知道为什么吗?谢谢!

推荐答案

没错,as 语法特定于 *ngIf.它不是一个可以在 Angular 模板中的任何地方使用的通用关键字.

That's correct, the as syntax is specific to *ngIf. It's not a general keyword you can use anywhere in Angular templates.

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

See https://angular.io/api/common/NgIf and search for NgIfAs class.

这篇关于异步管道在 Angular 的输入中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆