How to use a typescript enum value in an Angular2 ngSwitch statement

后端 未结 9 698
深忆病人
深忆病人 2020-12-07 16:13

The Typescript enum seems a natural match with Angular2\'s ngSwitch directive. But when I try to use an enum in my component\'s template, I get \"Cannot read property \'xxx

9条回答
  •  没有蜡笔的小新
    2020-12-07 16:59

    My component used an object myClassObject of type MyClass, which itself was using MyEnum. This lead to the same issue described above. Solved it by doing:

    export enum MyEnum {
        Option1,
        Option2,
        Option3
    }
    export class MyClass {
        myEnum: typeof MyEnum;
        myEnumField: MyEnum;
        someOtherField: string;
    }
    

    and then using this in the template as

    Do something for Option1
    Do something for Option2
    Do something for Opiton3

提交回复
热议问题