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

后端 未结 9 671
深忆病人
深忆病人 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:54

    Angular4 - Using Enum in HTML Template ngSwitch / ngSwitchCase

    Solution here: https://stackoverflow.com/a/42464835/802196

    credit: @snorkpete

    In your component, you have

    enum MyEnum{
      First,
      Second
    }
    

    Then in your component, you bring in the Enum type via a member 'MyEnum', and create another member for your enum variable 'myEnumVar' :

    export class MyComponent{
      MyEnum = MyEnum;
      myEnumVar:MyEnum = MyEnum.Second
      ...
    }
    

    You can now use myEnumVar and MyEnum in your .html template. Eg, Using Enums in ngSwitch:

    <div [ngSwitch]="myEnumVar">
      <div *ngSwitchCase="MyEnum.First"><app-first-component></app-first-component></div>
      <div *ngSwitchCase="MyEnum.Second"><app-second-component></app-second-component></div>
      <div *ngSwitchDefault>MyEnumVar {{myEnumVar}} is not handled.</div>
    </div>
    
    0 讨论(0)
  • 2020-12-07 16:56

    If using the 'typetable reference' approach (from @Carl G) and you're using multiple type tables you might want to consider this way :

    export default class AppComponent {
    
      // Store a reference to the enums (must be public for --AOT to work)
      public TT = { 
           CellType: CellType, 
           CatType: CatType, 
           DogType: DogType 
      };
    
      ...
    
      dog = DogType.GoldenRetriever; 
    

    Then access in your html file with

    {{ TT.DogType[dog] }}   => "GoldenRetriever"
    

    I favor this approach as it makes it clear you're referring to a typetable, and also avoids unnecessary pollution of your component file.

    You can also put a global TT somewhere and add enums to it as needed (if you want this you may as well make a service as shown by @VincentSels answer). If you have many many typetables this may become cumbersome.

    Also you always rename them in your declaration to get a shorter name.

    0 讨论(0)
  • 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

    <div [ngSwitch]="myClassObject.myEnumField">
      <div *ngSwitchCase="myClassObject.myEnum.Option1">
        Do something for Option1
      </div>
      <div *ngSwitchCase="myClassObject.myEnum.Option2">
        Do something for Option2
      </div>
      <div *ngSwitchCase="myClassObject.myEnum.Option3">
        Do something for Opiton3
      </div>
    </div>
    
    0 讨论(0)
提交回复
热议问题