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

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

问题描述

我知道在 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天全站免登陆