4

I was trying to define the enum in another file to use it in other places as well. For example on html view. Is this possible?

enums.model.ts

export enum MyEnum{
    First = 1,
    Second= 2
}

my-summary.component.ts

import {MyEnum} from "./app/models";
@Component({
    selector: "my-summary",
    templateUrl: "./my-summary.component.html"]
})
export class MySummaryComponent{ }

my-summary.component.html

   <div>
      {{MyEnum.First}}
   </div>

And it doesn't work. Module '"path/app/models/index"' has no exported member 'MyEnum'.

2
  • 2
    Possible duplicate of How to use enum in Angular 2 templates Commented Jan 11, 2018 at 13:55
  • what is in your index.ts file? If your dont have one then look at Sachila's answer below.. Path should be: "./app/models/enums.model" Having said that I've never been able to import a enum in angular 4. Commented Feb 2, 2018 at 17:37

1 Answer 1

12

Assign enum to a variable inside component.

import {MyEnum} from "./app/models";
@Component({
    selector: "my-summary",
    templateUrl: "./my-summary.component.html"]
})
export class MySummaryComponent{
   MyEnum = MyEnum;
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Still error ERROR in path/my-summary.component.ts(9,10): error TS2305: Module '"path/app/models/index"' has no exported member 'MyEnum'.
path should be import {MyEnum} from "./app/models/enums.mode";
any other way to achieve this globally? I have "CountryCodes" enum and I need to access it in structural directive that restricts block in various components by countrycodes and some other input params - so I will need to import this every time I want to use the directive(

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.