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

后端 未结 9 695
深忆病人
深忆病人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 16:39

    You can create a reference to the enum in your component class (I just changed the initial character to be lower-case) and then use that reference from the template (plunker):

    import {Component} from 'angular2/core';
    
    enum CellType {Text, Placeholder}
    class Cell {
      constructor(public text: string, public type: CellType) {}
    }
    @Component({
      selector: 'my-app',
      template: `
        
    {{cell.text}}
    Placeholder
    `, }) export default class AppComponent { // Store a reference to the enum cellType = CellType; public cell: Cell; constructor() { this.cell = new Cell("Hello", CellType.Text) } setType(type: CellType) { this.cell.type = type; } }

提交回复
热议问题