Read the TypeDecorator from Angular 5 component

Viewed 349

Previously, I was able to go get the metadata for a component with the Reflect metadata. Now the metadata has been moved to some annotations attached to the component.

Reading the metadata as such

type['__annotations__'][0].selector

is definitely a hack. What is the correct method to read such metadata from a component? I want to obtain the data attached to a component in the @Component decorator.

1 Answers

If what you need is the selector, this is a fine way to get it.

constructor(public elementRef: ElementRef<any>) {}
...
const nodeName = this.elementRef.nativeElement.nodeName.toLowerCase();
...

I have a component that can either be declared as an element or as an attribute. This approach lets me know if the host element is a div (when my component is an attribute) or my component's selector.

Related