Skip to content Skip to sidebar Skip to footer

How To Check If A Component Exists In Angular 1.5

http://embed.plnkr.co/oGlcQSOM2vFcDEKP7thV/ $injector.has('myMessageDirective') returns true, while $injector.has('myMessageComponent') does not Is anyone struggling with this or h

Solution 1:

At the end of the day, a component is registered as a directive, so 'Directive' suffix is indeed needed.

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.

Solution 2:

I think, simplest way is to add an id attribute to directive's/component's container, then check if element with this id exist.

Post a Comment for "How To Check If A Component Exists In Angular 1.5"