如何检查 Angular 1.5 中是否存在组件 [英] How to check if a component exists in Angular 1.5
问题描述
http://embed.plnkr.co/oGlcQSOM2vFcDEKP7thV/
$injector.has('myMessageDirective')
返回 true,而 $injector.has('myMessageComponent')
不
$injector.has('myMessageDirective')
returns true, while $injector.has('myMessageComponent')
does not
是否有人为此苦苦挣扎或有解决方案?我担心"的是,由于指令检查,我的组件可能在未来的更新中找不到.
Is anyone struggling with this or has a solution? My "fear" is that my components might not be found in future updates because of the directive check.
跟进问题:检查 Angular 指令是否存在
推荐答案
归根结底,组件注册为指令,因此确实需要'Directive'后缀.
At the end of the day, a component is registered as a directive, so 'Directive' suffix is indeed needed.
检查$injector对象的'has'方法:
Check 'has' method of $injector object:
return {
invoke: invoke,
instantiate: instantiate,
get: getService,
annotate: createInjector.$$annotate,
has: function(name) {
return providerCache.hasOwnProperty(name + providerSuffix) || cache.hasOwnProperty(name);
}
};
您可以调试它并看到所有组件(在缓存对象内)都注册为指令.没有组件"后缀.
You can debug it and see that all components (inside cache object) are registered as directives. There is no 'Component' suffix.
这篇关于如何检查 Angular 1.5 中是否存在组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!