指令中的异步HostBinding [英] Async HostBinding in directive

查看:54
本文介绍了指令中的异步HostBinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用异步值处理HostBinding的最佳方法.

I'm looking for the best way to handle HostBinding with async value.

在Angular v2.1.2 之前,我可以像这样在@Directive装饰器中使用host属性:

Before Angular v2.1.2 I could use the host property in the @Directive decorator like that :

@Directive({
    selector: 'img[my-directive]',
    host    : {
        '[alt]'  : "alt | async"
    }
})
export class MyDirective {
    alt: Observable<string>;
}

但是,这似乎不是预期的行为,因为版本2.1.2修复了该问题.请参见不要访问视图局部变量或宿主表达式中的管道.

But it looks like this was not the intended behavior, since version 2.1.2 fixes it. See don't access view local variables nor pipes in host expressions.

现在,使用AoT编译进行编译时,我得到Parser Error: Host binding expression cannot contain pipes in Directive.

Now, when compiling with AoT compilation, I get Parser Error: Host binding expression cannot contain pipes in Directive.

推荐答案

Tobias Bosch(Angular团队的成员)写道:

Tobias Bosch (member of the Angular team) writes:

组件(子")的主机绑定在组件中执行 使用该组件(父").父组件可以 属于另一个NgModule.因此,如果您使用管道,则管道是 针对父组件的NgModule进行了解析.但是,如果 NgModule没有声明您正在使用的管道,您的 组件损坏.

Host bindings of a component ("child") are executed in the component that uses that component ("parent"). And the parent component can belong to a different NgModule. So if you use a pipe, the pipe is resolved against the NgModule of the parent component. However, if that NgModule does not declare the pipe that you are using, your component is broken.

这就是为什么我们从不希望在主机绑定中使用管道. 在2.0最终版之前进行了较大的编译器重构之后,我们 不小心重新引入了它,但这是一个错误,而不是功能,因为 语义是错误的.

This is the reason why we never wanted to have pipes in host bindings. After one of the bigger compiler refactorings before 2.0 final, we accidentally reintroduced it, but this was a bug, not a feature, as the semantics are wrong.

来源:

异步主机绑定不再起作用#12671

这篇关于指令中的异步HostBinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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