1

I have an enum:

export enum NumberEnum { 
   One = 1,
   Two = 2,
   Three = 3
}

Within my component I have a successfully working call to a service which is passing back a result which has the associated properties returned with the enum values as integers:

export class DetailsComponent implements OnInit, OnDestroy {

details$: any;
number: NumberEnum;


ngOnInit(): void {
         this.route.params.pipe(
         switchMap(params => this.service.getDetails(params['details-id']))
       ).subscribe(result => {
         this.details$ = result;
         this.number = this.details$.Number as NumberEnum;
       });
}

In my html when I try to display {{ number }} it is displaying the integer value as opposed to the string value which I would like. I have tried casting a different way...which looks like this:

this.number = NumberEnum[this.details$.Number];

but i get an error "Type 'string' is not assignable to type 'Number'" The code compiles and displays as I would like, I just know it's not best practise to have a red error line in my code.

Can't seem to find anything online which achieves what I want to do.

3 Answers 3

4

rather then setting this.number = NumberEnum[this.details$.Number]; and changing your this.number, just display it differently in the HTML. Either do {{ NumberEnum[number];}} or if that does not work, create a pipe that does it and add it like {{number|stringPipe}}

Sign up to request clarification or add additional context in comments.

3 Comments

This does work, is there any benefit to manipulating it in the html rather than in the component?
yes. You are just affecting what you display, without affecting the value in your component. This can help prevent undesired side affects and the like later on
that makes sense, this is purely a display component though there is no manipulation of the data on the front end. I will mark this as the answer as this does work
1

Given that the type of details$ is string, I would add a function in DetailsComponent that will return the right NumberEnum value.

 export class DetailsComponent implements OnInit, OnDestroy {
    
    details: string;
    number: NumberEnum | undefined;
    
    
    ngOnInit() {
             this.route.params.pipe(
             switchMap(params => this.service.getDetails(params['details-id']))
           ).subscribe(result => {
             this.details = result;
             this.number = this.getDetailsNumber(this.details);
           });
    }
    getDetailsNumber(value: string): NumberEnum | undefined {
      return Number(value) in NumberEnum ? NumberEnum[Number(value)] : undefined;
    }
}

2 Comments

Is there no way to cast it so that a method is not required? This does work however
Casting is required because your Enum type is different than your service response in the specific example. You can modify the type to Number in your service response and the casting is no more needed.
0

Ok so it turns out that the reason it was compiling ok with the red error line is that this was actually just a tsLint error (other errors were shown in yellow beforehand so not sure why this one was red - must've been my theme). Anyway I solved this by declaring number as a string instead of an enum so when I casted it, it accepted it and cast the numerical value to the enum string. And it displayed properly. Shown below:

export class DetailsComponent implements OnInit, OnDestroy {

details$: any;
number: string;

this.number = NumberEnum[this.details$.Number];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.