0

When I reference to an enum value I am getting below error.

ERROR TypeError: Cannot read property 'Text' of undefined

Is there any way to use this enum within html part of component?

My Enum;

 export enum InputType {
    Text = "text",
    Number = "number",
    Color = "color"
}

My component;

export class AppComponent {
  title = 'app';

  inputType : InputType;

}

html part;

<app-input [inputType]="inputType.Text"></app-input>
1
  • 2
    You need to assign the inputType -> inputType = InputType;. Otherwise, inputType is declared as type "InputType" but is never assigned, hence it will be undefined at runtime. Commented Sep 12, 2018 at 8:36

1 Answer 1

2

Change:

export class AppComponent {
  title = 'app';

  inputType : InputType;

}

to:

export class AppComponent {
  title = 'app';

  inputType = InputType;

}

Otherwise, inputType will be treated by typescript as an InputType, but will never be assigned, hence it will be undefined at runtime (and angular will throw the exception as stated above, since it's trying to access a property of an undefined component property).

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

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.