在另一个指令的宿主中使用指令 [英] Use directive in host of another Directive

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

问题描述

我想通过使用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 { }

执行此操作时,出现标准无法绑定到'两个',因为它不是已知的本机属性"错误.

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?

推荐答案

只有针对与静态添加的HTML(元素,id,属性,类,...)匹配的选择器,Angular会实例化指令.
无法使用@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屋!

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