2017-04-22 13 views
0

角度2を初めて使っていますが、これを使ってアプリケーションを作成しています。私はCSharpのバックグラウンドから来ているので、enum型のプロパティを作成できると仮定します。その場所に基づいて特定のenum値を適用するようにします。角度2でカスタム列挙型プロパティを使用する方法

しかし、私はどのようにそのような技術を達成するために説明するオンラインの例を発見していない。デモを提供することは可能ですか?

export enum DisplayType { 
    Small, 
    Large 
} 

<e-view displayType="DisplayType.Small"></e-view> 

可能であれば、同じ結果を達成できる他の手法がありますか?

答えて

0
あなたが代わりにあなたが列挙型の値に

<e-view [displayType]="getType()"></e-view> 
getType(){ 
    return DisplayType.Small 
} 

注意を取得する方法を使用する必要があり、HTMLで次のように変数名を処理することができません

:あなたの質問displayTypeで[ ]

+0

enumsはそのような目的のために使用することができません、ok ...しかし、私はそのようにビューコンポーネントの表示タイプを指定したいと思います: 。 displayTypeの型がstringの場合、htmlテンプレートにどのように適用できますか? – vicgoyso

+0

文字列を持つようにenumを定義する方法 – Aravind

+0

いいえ、今は代わりに文字列を使用することを検討しています。enumを使用できないためです。私はどのように私のHTMLテンプレートに値 "小さな"を渡すことができたか知りたいですか? – vicgoyso

0
export enum AgentStatus { 
    available =1 , 
    busy = 2, 
    away = 3, 
    offline = 0 
} 
を持っていませんでした

文字列をTypeScriptの列挙型に変換するにはどうすればよいですか?

var value = AgentStatus["offline"]; // so value is now 0 

// You can also use this, which only gives IDE hints, no runtime benefit 
var value: AgentStatus = AgentStatus["offline"]; 

TypeScript列挙型の値をすべて取得するにはどうすればよいですか?

var options : string[] = Object.keys(AgentStatus); 
// The options list has the numeric keys, followed by the string keys 
// So, the first half is numeric, the 2nd half is strings 
options = options.slice(options.length/2); 

あなたAngular2テンプレートでこれを記述する場合:

{{AgentStatus[myValue]}} 

それは輸入のタイプ(それはAngularJSで、後に実行されます)へのアクセス権を持っていないので、それは失敗します。

それを動作させるために、あなたのコンポーネントが列挙型/オブジェクトへの参照を持っている必要があります、のようなもの:

export class MyComponent { 
    // allows you to use AgentStatus in template 
    AgentStatus = AgentStatus;   

    myValue : AgentStatus; 
    // ... 
} 

例:

import { Component } from '@angular/core'; 

enum AgentStatus { 
    available =1 , 
    busy = 2, 
    away = 3, 
    offline = 0 
} 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <h1>Choose Value</h1> 

    <select (change)="parseValue($event.target.value)"> 
    <option>--select--</option> 
    <option *ngFor="let name of options" 
     [value]="name">{{name}}</option> 
    </select> 

    <h1 [hidden]="myValue == null"> 
    You entered {{AgentStatus[myValue]}} 
    <br> 
    You are {{isOffline ? "offline" : "not offline"}}. 
    </h1>` 
}) 
export class AppComponent { 


    options : string[]; 
    myValue: AgentStatus; 
    AgentStatus : typeof AgentStatus = AgentStatus; 
    isOffline : bool; 

    ngOnInit() { 
    var x = AgentStatus; 
    var options = Object.keys(AgentStatus); 
    this.options = options.slice(options.length/2); 
    } 

    parseValue(value : string) { 
    this.myValue = AgentStatus[value]; 
    this.isOffline = this.myValue == AgentStatus.offline; 
    } 
} 
0
あなたのコンポーネントに

を追加し、もう1つのゲッターを追加してください

get DisplayType() { 
    return DisplayType; 
} 

、あなたはプロパティを追加、列挙型を使用してコンポーネントでは、あなたのテンプレートに

[displayType]="DisplayType.Small" 
0

を使用することができます。

readonly DisplayType: typeof DisplayType = DisplayType; 

は、次にHTMLで列挙型を呼び出す:

<e-view [displayType]="DisplayType.Small"></e-view> 
関連する問題