【问题标题】:Use directive in host of another Directive在另一个指令的宿主中使用指令
【发布时间】:2016-09-05 23:53:36
【问题描述】:

我想通过使用主机属性将指令添加到另一个指令中的元素,但似乎没有办法引用另一个指令。

@Directive({
    selector: '[one]',
    host: { '[two]': '"some-value"' }
    // How can I reference DirectiveTwo here?
})
export class DirectiveOne { }

@Directive({
    selector: '[two]'
})
export class DirectiveTwo { }

执行此操作时,我收到标准“无法绑定到‘两个’,因为它不是已知的本机属性”错误。

从另一个指令引用和使用一个指令的正确方法是什么?

【问题讨论】:

    标签: angular angular2-directives


    【解决方案1】:

    指令仅由 Angular 实例化,用于匹配静态添加的 HTML(元素、id、属性、类...)的选择器。
    无法使用@Component()@Directive() 装饰器的host 参数来实例化指令。也无法使用ViewContainerRef.createComponent()(前DynamicComponentLoader)动态创建指令

    支持获取对另一个指令的引用,该指令由 Angular 实例化,因为在同一元素上静态添加了 HTML。

    【讨论】:

    • 如果是这样,让一个指令扩展或使用其他指令的功能的最佳做法是什么?这似乎是一个非常重要的限制。
    • 我认为这是一个更大的更新主题github.com/angular/angular/issues/11606
    猜你喜欢
    • 2016-09-26
    • 2018-05-01
    • 2023-03-29
    • 2013-11-23
    • 1970-01-01
    • 2016-11-06
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多