6

What I want (pseudocode):

@Component({
  selector: 'app-some-component',
  ....
})
export class SomeComponentComponent { 

    constructor() {
        console.log(@COMPONENT.SELECTOR); // logs 'app-some-component'
    }

    ....
}

Background:

I have a simple component:

@Component({
  selector: 'app-some-component',
  ....
})
export class SomeComponentComponent { ....

Now inside this component, I want to create an unique identifier which should contain some the name of the component.

Example:

'app-some-component_' + UuidUtil.generateUUID();

obviously it would be nicer if I do this with:

SELECTOR + '_' + UuidUtil.generateUUID();

but then I have to declare the selector as variable outside and it I get problems because my IDE (Webstorm) teslls me unkown selector in the *.html template.

How can I achieve this.

@duplication The solution posted there is pretty hacky because requires element ref which is cast to lowercase. I hope now, after 2 years, there is a better way. Maybe also due to the further development in typescript.

Stefan
  • 14,826
  • 17
  • 80
  • 143
  • 1
    Possible duplicate of [Accessing \`selector\` from within an Angular 2 component](https://stackoverflow.com/questions/37188216/accessing-selector-from-within-an-angular-2-component) – Harun Yilmaz Apr 05 '19 at 10:26
  • Edit: addressed duplication. But thanks, I had a different title before under which I didn't find it. – Stefan Apr 05 '19 at 10:32

0 Answers0