How to access an enum value in an Angular 5 view

前端 未结 3 1388
我在风中等你
我在风中等你 2020-12-19 00:06

I am currently referencing the enum int value directly in my html view, but I would prefer to reference by the enum name - for example, as I do in

3条回答
  •  死守一世寂寞
    2020-12-19 00:38

    Since the expression context of the template is the component instance, you should assign the NodeType enum to a property of the component class to make it available in the template:

    export class ConfigNetworkTreeComponent {
      public NodeTypeEnum = NodeType;  // Note: use the = operator
      ...
    }
    

    You can then use that property in the template to refer to NodeType values:

    *ngIf="selectedNode && selectedNodeType === NodeTypeEnum.HostService"
    

    See this stackblitz for a demo.

提交回复
热议问题