2017-05-18 5 views
3

MyService.service.ts MyComponent.component.tsとMyComponent.component.htmlで使用するTypescriptで列挙しました。HTML内のTypescript列挙型に近づくことはできません

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; 
} 

私も* ngIf文を使用して、私のHTML内の比較のために列挙型を使用したいです:

<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

enter image description here

次のHTML指示誤差ラインで:

enter image description here

列挙型は次のように近づくことができない理由を誰もが知っていますか?

答えて

7

テンプレートの有効範囲は、コンポーネントのインスタンスメンバーに限定されます。あなたが何かを参照したい場合は、

*ngIf="connectionResult.Success" 

Angular2 access global variables from HTML template

も参照して使用することができます は、それがその後、

class MyComponent { 
    connectionResult = ConnectionResult; 
} 

利用できるようにする必要があります

関連する問題