在另一个指令的主机中使用指令 [英] Use directive in host of another Directive
问题描述
我想通过使用 host 属性向另一个指令中的元素添加一个指令,但似乎没有办法引用另一个指令.
I want to add a directive to an element from another directive by using the host property, but there doesn't seem to be a way to reference the other directive.
@Directive({
selector: '[one]',
host: { '[two]': '"some-value"' }
// How can I reference DirectiveTwo here?
})
export class DirectiveOne { }
@Directive({
selector: '[two]'
})
export class DirectiveTwo { }
执行此操作时,我收到标准的无法绑定到 'two',因为它不是已知的本机属性"错误.
When doing this, I get the standard "Can't bind to 'two' since it isn't a known native property" error.
从另一个指令引用和使用一个指令的正确方法是什么?
What is the correct way of referencing and using one directive from another?
推荐答案
指令由 Angular 实例化,用于仅匹配静态添加的 HTML(元素、ID、属性、类...)的选择器.
无法使用 @Component()
或 @Directive()
装饰器的 host
参数实例化指令.也无法使用 ViewContainerRef.createComponent()
(以前的 DynamicComponentLoader
)
Directives are instantiated by Angular for selectors matching statically added HTML (element, id, attribute, class, ...) only.
There is no way to instantiate directives using the host
parameter of the @Component()
or @Directive()
decorator. There is also no way to create directives dynamically using ViewContainerRef.createComponent()
(former DynamicComponentLoader
)
虽然支持在同一元素上静态添加 HTML,因此获取对另一个由 Angular 实例化的指令的引用.
Getting a reference to another directive that was instantiated by Angular because of a statically added HTML on the same element is supported though.
这篇关于在另一个指令的主机中使用指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!