Typescript枚举看起来和Angular2的ngSwitch指令很匹配。但是当我试图在我的组件的模板中使用枚举时,我得到“无法读取属性'xxx'的未定义在…”如何在组件模板中使用枚举值?
请注意,这与如何基于枚举(ngFor)的所有值创建html选择选项不同。这个问题是关于基于枚举的特定值的ngSwitch。尽管出现了创建枚举的类内部引用的相同方法。
Typescript枚举看起来和Angular2的ngSwitch指令很匹配。但是当我试图在我的组件的模板中使用枚举时,我得到“无法读取属性'xxx'的未定义在…”如何在组件模板中使用枚举值?
请注意,这与如何基于枚举(ngFor)的所有值创建html选择选项不同。这个问题是关于基于枚举的特定值的ngSwitch。尽管出现了创建枚举的类内部引用的相同方法。
当前回答
你现在可以这样做:
例如,枚举为:
export enum MessagePriority {
REGULAR= 1,
WARNING,
IMPORTANT,
}
一个状态消息,看起来像这样:
export default class StatusMessage{
message: string;
priority: MessagePriority;
constructor(message: string, priority: MessagePriority){
this.message = message;
this.priority = priority;
}
}
然后在组件的.ts文件中,你可以这样做:
import StatusMessage from '../../src/entities/building/ranch/administration/statusMessage';
import { MessagePriority } from '../../enums/message-priority';
export class InfoCardComponent implements OnInit {
messagePriority: typeof MessagePriority;
constructor() {
this.messagePriority = MessagePriority;
}
@Input() statusMessage: StatusMessage;
ngOnInit(): void {}
}
最后这个组件的HTML是这样的:
<div class="info-card" [ngSwitch]="statusMessage.priority">
<h2 *ngSwitchCase="this.messagePriority.REGULAR" class="info-card__regular-message">{{statusMessage.message}}</h2>
<h2 *ngSwitchCase="this.messagePriority.WARNING" class="info-card__warning-message">{{statusMessage.message}}</h2>
<h2 *ngSwitchCase="this.messagePriority.IMPORTANT" class="info-card__important-message">{{statusMessage.message}}</h2>
</div>
注意,枚举首先用“typeof messagpriority”类型声明给类,然后用“this”调用定义来绑定到类。messagpriority = messagpriority "
其他回答
这很简单,而且很有魅力:) 只是像这样声明你的枚举,你可以在HTML模板上使用它
statusEnum: typeof StatusEnum = StatusEnum;
作为@Eric Lease的装饰器(不幸的是,它不能使用——aot(因此也不能使用——prod)构建)的替代方案,我求助于使用一个公开应用程序所有枚举的服务。只需要公开地将它注入到每个需要它的组件中,在一个简单的名称下,之后你可以在视图中访问枚举。例如:
服务
import { Injectable } from '@angular/core';
import { MyEnumType } from './app.enums';
@Injectable()
export class EnumsService {
MyEnumType = MyEnumType;
// ...
}
不要忘记将它包含在模块的提供者列表中。
组件类
export class MyComponent {
constructor(public enums: EnumsService) {}
@Input() public someProperty: MyEnumType;
// ...
}
html组件
<div *ngIf="someProperty === enums.MyEnumType.SomeValue">Match!</div>
你现在可以这样做:
例如,枚举为:
export enum MessagePriority {
REGULAR= 1,
WARNING,
IMPORTANT,
}
一个状态消息,看起来像这样:
export default class StatusMessage{
message: string;
priority: MessagePriority;
constructor(message: string, priority: MessagePriority){
this.message = message;
this.priority = priority;
}
}
然后在组件的.ts文件中,你可以这样做:
import StatusMessage from '../../src/entities/building/ranch/administration/statusMessage';
import { MessagePriority } from '../../enums/message-priority';
export class InfoCardComponent implements OnInit {
messagePriority: typeof MessagePriority;
constructor() {
this.messagePriority = MessagePriority;
}
@Input() statusMessage: StatusMessage;
ngOnInit(): void {}
}
最后这个组件的HTML是这样的:
<div class="info-card" [ngSwitch]="statusMessage.priority">
<h2 *ngSwitchCase="this.messagePriority.REGULAR" class="info-card__regular-message">{{statusMessage.message}}</h2>
<h2 *ngSwitchCase="this.messagePriority.WARNING" class="info-card__warning-message">{{statusMessage.message}}</h2>
<h2 *ngSwitchCase="this.messagePriority.IMPORTANT" class="info-card__important-message">{{statusMessage.message}}</h2>
</div>
注意,枚举首先用“typeof messagpriority”类型声明给类,然后用“this”调用定义来绑定到类。messagpriority = messagpriority "
您可以创建一个自定义装饰器,添加到组件中,使其能够识别枚举。
myenum.enum.ts:
export enum MyEnum {
FirstValue,
SecondValue
}
myenumaware.decorator.ts
import { MyEnum } from './myenum.enum';
export function MyEnumAware(constructor: Function) {
constructor.prototype.MyEnum = MyEnum;
}
enum-aware.component.ts
import { Component } from '@angular2/core';
import { MyEnum } from './myenum.enum';
import { MyEnumAware } from './myenumaware.decorator';
@Component({
selector: 'enum-aware',
template: `
<div [ngSwitch]="myEnumValue">
<div *ngSwitchCase="MyEnum.FirstValue">
First Value
</div>
<div *ngSwitchCase="MyEnum.SecondValue">
Second Value
</div>
</div>
<button (click)="toggleValue()">Toggle Value</button>
`,
})
@MyEnumAware // <---------------!!!
export default class EnumAwareComponent {
myEnumValue: MyEnum = MyEnum.FirstValue;
toggleValue() {
this.myEnumValue = this.myEnumValue === MyEnum.FirstValue
? MyEnum.SecondValue : MyEnum.FirstValue;
}
}
如果使用“typetable reference”方法(来自@Carl G),并且你正在使用多个类型表,你可能会考虑这样做:
export default class AppComponent {
// Store a reference to the enums (must be public for --AOT to work)
public TT = {
CellType: CellType,
CatType: CatType,
DogType: DogType
};
...
dog = DogType.GoldenRetriever;
然后在html文件中访问
{{ TT.DogType[dog] }} => "GoldenRetriever"
我喜欢这种方法,因为它清楚地表明您引用的是一个类型表,而且还避免了对组件文件的不必要污染。
你也可以在某个地方放置一个全局TT,并根据需要添加枚举(如果你想这样做,你也可以像@VincentSels answer所示的那样创建一个服务)。如果你有很多很多的类型,这可能会变得很麻烦。
此外,您总是在声明中重命名它们以获得更短的名称。