如何以编程方式实例化和应用指​​令? [英] How to instantiate and apply directives programmatically?

查看:60
本文介绍了如何以编程方式实例化和应用指​​令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在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屋!

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