无法在HTML中使用Typescript枚举

我使用Typescript创建了一个枚举,用于MyService.service.ts MyComponent.component.ts和MyComponent.component.html.
export enum ConnectionResult {
    Success,Failed     
}

我可以轻松地从MyService.service.ts获取并比较定义的枚举变量:

this.result = this.myService.getConnectionResult();

switch(this.result)  
{
    case ConnectionResult.Failed:
         doSomething();
         break;
    case ConnectionResult.Success:
         doSomething();
         break;
}

我还想使用enum在我的HTML中使用* ngIf语句进行比较:

<div *ngIf="result == ConnectionResult.Success; else failed">
            <img src="../../assets/connection-success.png" height="300px" class="image-sign-style" />
</div>
<ng-template #failed>
       <img src="../../assets/connection-failed.png" height="300px" class="image-sign-style" />
</ng-template>

代码编译但浏览器给我一个错误:

Cannot read property of undefined

使用以下html指示错误行:

有谁知道为什么不能像这样接近枚举?

解决方法

模板的范围仅限于组件实例成员.
如果你想参考那里需要的东西
class MyComponent {
  connectionResult = ConnectionResult;
}

那你可以用

*ngIf="connectionResult.Success"

另见Angular2 access global variables from HTML template

相关文章

我最大的一个关于TypeScript的问题是,它将原型的所有方法(无...
我对React很新,我正在尝试理解子组件之间相互通信的简洁方法...
我有一个非常简单的表单,我将用户电子邮件存储在组件的状态,...
我发现接口非常有用,但由于内存问题我需要开始优化我的应用程...
我得到了一个json响应并将其存储在mongodb中,但是我不需要的...
我试图使用loadsh从以下数组中获取唯一类别,[{"listing...