export enum Type {
TYPE_1 : "Apple",
TYPE_2 : "Orange",
TYPE_3 : "Banana"
}
当我登录时Type.TYPE_1
, toString
默认情况下会调用方法。
console.log(Type.TYPE_1 + " is " + Type.TYPE_1.toString());
Output => Apple is Apple
我的期望是结果就像
Output : TYPE_1 is Apple
我如何登录/获取密钥TYPE_1
作为字符串?
有办法做吗override method
像下面这样?
export enum Type {
TYPE_1 : "Apple",
TYPE_2 : "Orange",
TYPE_3 : "Banana"
toString() {
this.key + " is " + this.toString();
<or>
this.key + " is " + this.value();
}
}
我已经在谷歌上搜索了,但我还不太确定。
Update
目的是在UI中显示
export enum Currency {
USD : "US Dollar",
MYR : "Malaysian Ringgit",
SGD : "Singapore Dollar",
INR : "Indian Rupee",
JPY : "Japanese Yen"
}
currencyList : Currency[]= [Currency.USD, Currency.MYR, Currency.SGD, Currency.INR, Currency.JPY];
<table>
<tr *ngFor="let currency of currencyList">
<td>
<input name="radioGroup" type="radio" [(ngModel)]="selectedType" [value]="currency">
<label>{{currency}} is {{currency.toString()}}</label>
<!--
here expectiation is Example
USD is US Dollar
MYR is Malaysian Ringgit
SGD is Singapore Dollar
....
Now I get "US Dollar is US Dollar"....
-->
</td>
</tr>
</table>