我从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中访问’type’时,我只得到一个int值.但是我想获得一个字符串值.
例如:
'message.type=0'
{{message.type}} => should be Info
'message.type=1'
{{message.type}} => should be Warning
解决方法:
TypeScript中的枚举是运行时的数字,因此message.type将为0,1,2或3.
要获取字符串值,您需要将该数字作为索引传递给枚举:
Type[0] // "Info"
所以,在你的例子中,你需要这样做:
Type[message.type] // "Info" when message.type is 0
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。