简体   繁体   中英

Angular 2 ng2 How to use TypeScript enum inside template?

I have component for multiple filters with different types. For type recognition I want to use enum with filter types. How to use enum inside template when sample below does'nt work?

I thought it should work by only importing enum inside component where I want to use this enum.

import { FilterType } from './types/FilterType';

And use it inside template like FilterType.INPUT_SELECT and FilterType.INPUT_TEXT but it does'nt work then I used variable but it also does'nt work.

<div *ngFor='let filter of filters'>
  <select *ngIf='filter.type === checkType.INPUT_SELECT'>...</select>
  <input *ngIf='filter.type === checkType.INPUT_TEXT'></input>
</div>
...
export class FiltersComponent {
  checkType: FilterType;
  @Input() filters: any[];
}
...
export enum FilterType {
  INPUT_SELECT,
  INPUT_TEXT
}

您需要为`checkType指定一个值:

checkType: FilterType = FilterType;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM