我从 RESTful 服务获得以下数据:
[
{
"id": 42,
"type": 0,
"name": "Piety was here",
"description": "Bacon is tasty, tofu not, ain't nobody like me, cause i'm hot...",
}...
我正在使用这个类进行映射:
export enum Type {
Info,
Warning,
Error,
Fatal,
}
export class Message{
public id: number;
public type: Type:
public name: string;
public description: string;
}
但是当我在 Angular2 中访问“类型”时,我只得到一个 int 值。但我想得到一个字符串值。
例如:
'message.type=0'
{{message.type}} => should be Info
'message.type=1'
{{message.type}} => should be Warning
原文由 Franz Peter Tebartz van Elst 发布,翻译遵循 CC BY-SA 4.0 许可协议
Enums in TypeScript are numbers at runtime, so
message.type
will be0
,1
,2
or3
.要获取字符串值,您需要将该数字作为索引传递到枚举中:
因此,在您的示例中,您需要这样做:
文档