2017-06-16 2 views
0

こんにちは私は簡単な質問があります。私のコンポーネントには1つの入力があり、値の挿入方法を指定したいと思います。特定の値を持つAngular2コンポーネント入力

export class MyComponent{ 
    @Input() type: string; //only active, disable... 
} 

どのように値が有効であると言うことができますか?

enum State { 
    Active, 
    Disabled 
} 

とオン:ありがとう

+0

はい、[Enum](https://www.typescriptlang.org/docs/handbook/enums.html)を使用できますが、スイッチのケースや条件を使用することをお勧めします。あなたの 'ngOnInit' – trichetriche

答えて

0

一つの方法は、あなたが列挙型

https://www.typescriptlang.org/docs/handbook/enums.html

がような何かを使用することができますプロパティsetter

export class MyComponent{ 
     private _type: string; //only active, disable... 


     get type(): string{ 
      return this._type; 
     } 

     @Input() set type(value: string) { 
      if(['active', 'disable'].indexOf(value) !== -1) 
       this._type = value; 
      else 
       // take action   
     } 
} 
0

に入力値を検証することですコンポーネント

@Input() type: State; 
関連する問題