1

I am saving the numeric value of an enum in my database and returning it to the UI in a different area. Now I want to extract the string value associated with that numeric value in the enum in a different angular component.

I have an enum like so:

export enum ReportedResourceStatusEnum {
    New = 1,
    Reviewed = 2,
    Escalated = 3,
    Resolved = 4
}

That value (the 1, 2, 3, 4) is stored as a string property (nvarchar) in the table. When I call the api, an object is returned like this:

{
   something: 'Something',
   status: '1'
}

When I display to the UI, the '1' is displayed. How do I inject the Enum class and extract the string associated with the value of 1. In this case, being the word New.

1 Answer 1

1

Syntax is: ReportedResourceStatusEnum[1]

Try this:

export enum ReportedResourceStatusEnum {
  New = 1,
  Reviewed = 2,
  Escalated = 3,
  Resolved = 4
}


export class AppComponent {
  data = {
    something: 'Something',
    status: '1'
  }

  constructor() {
    console.log(ReportedResourceStatusEnum[Number(this.data.status)]) //New
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you.

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.