如何以编程方式实例化和应用指令? [英] How to instantiate and apply directives programmatically?
问题描述
我知道在ng2中,我们有ComponentFactoryResolver
可以解析可应用于ViewContainerRef
的工厂.
I know that in ng2 we have ComponentFactoryResolver
that can resolve factories that we can apply to a ViewContainerRef
.
但是,指令是否有类似的东西?一种实例化它们并将它们应用于组件中投影内容的方法?
But, is there something similar for directives? a way to instantiate them and apply them to the projected content from a component?
推荐答案
否,不能动态添加或删除指令.它们仅适用于静态添加到组件模板的HTML.
No, directives can't be added or removed dynamically. They are only applied to HTML statically added to component templates.
您可以做的是通过将参数(@Input()
)传递给指令来通知它是否执行某项操作,以启用/禁用该指令.
What you could do is to enable/disable the directive by passing a parameter (@Input()
) to the directive to notify it to do something or not.
这篇关于如何以编程方式实例化和应用指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!