Cannot approach Typescript enum within HTML

前端 未结 5 439
生来不讨喜
生来不讨喜 2021-01-31 13:03

I made an enum with Typescript to use in MyService.service.ts MyComponent.component.ts and MyComponent.component.html.

export enum ConnectionResult {
    Succes         


        
5条回答
  •  孤城傲影
    2021-01-31 13:53

    The scope of the template is limited to the component instance members. If you want to refer to something it needs to be available there

    class MyComponent {
      public get connectionResult(): typeof ConnectionResult {
        return ConnectionResult; 
      }
    }
    

    In the HTML you can now use

    *ngIf="connectionResult.Success"
    

    See also Angular2 access global variables from HTML template

提交回复
热议问题