我看过这样的问题。但这并不能完全回答我的问题。我想将局部变量名称绑定到枚举的值,如以下(高度简化)示例:
在certain-state.component.ts
:
export enum CertainState {
VALID,
INVALID
}
export class CertainStateComponent {
// holder for the current state
public state: CertainState;
// store the actual enum in the instance of the class so that
// it is available in the template
public certainStates: typeof CertainState = CertainState;
// below is the logic which sets the state
...
}
在certain-state.component.html
:
<ng-container *ngTemplateOutlet="state_{{state}}"></ng-container>
// obviously this is invalid syntax but I want to demonstrate my intention
<ng-template #state_{{certainStates.VALID}}><span>VALID</span></ng-template>
<ng-template #state_{{certainStates.INVALID}}><span>INVALID</span></ng-template>
编辑: 我认为解决方案在以下答案中:How to use a typescript enum value in an Angular2 ngSwitch statement。你们有什么感想?