简体   繁体   English

通过名称获取角度指令

[英]Get angular directive by name

I know that we can get services from $injector by their name. 我知道我们可以通过$injector的名称获得服务。 Is it possible to somehow get the directive/component object from the dependency injector? 是否有可能以某种方式从依赖项注入器获取指令/组件对象?

For example, if I wanted to write a service that can take a component's name, retrieve the component and do something with retrieved properties, how would I be able to retrieve the component by its name? 例如,如果我想编写一个可以使用组件名称,检索该组件并对检索到的属性进行某些处理的服务,那么我将如何通过其名称来检索该组件?

You can get services from $injector because there is only a single instance of each service in the system: they are singletons . 您可以从$injector获得服务,因为系统中每个服务只有一个实例: 它们是singletons

On the other hand, Directives/components have multiple instances for each time they show up on the page. 另一方面, 指令/组件每次在页面上显示时都有多个实例。 Moreover, they cannot be injected as they are always created and bound to HTML. 此外,由于它们总是被创建并绑定到HTML,因此无法注入它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM