2017-06-16 26 views
0

您好,我有一個簡單的問題。我的組件有一個輸入,我想指定如何插入值。帶有特定值的Angular2組件輸入

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

可以說輸入值是如何有效的?由於

+0

是的,你可以使用[Enum](https://www.typescriptlang.org/docs/handbook/enums.html),但我認爲最好的方法是使用switch case或條件你的'ngOnInit' – trichetriche

回答

0

一種方法是在財產驗證輸入值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   
     } 
}