我们可以在angular2视图模板中使用枚举吗?
<div class="Dropdown" dropdownType="instrument"></div>
传递字符串作为输入:
enum DropdownType {
instrument,
account,
currency
}
@Component({
selector: '[.Dropdown]',
})
export class Dropdown {
@Input() public set dropdownType(value: any) {
console.log(value);
};
}
但是如何通过枚举配置呢?我想要在模板中这样的东西:
<div class="Dropdown" dropdownType="DropdownType.instrument"></div>
最佳做法是什么?
编辑:创建了一个示例:
import {bootstrap} from 'angular2/platform/browser';
import {Component, View, Input} from 'angular2/core';
export enum DropdownType {
instrument = 0,
account = 1,
currency = 2
}
@Component({selector: '[.Dropdown]',})
@View({template: ''})
export class Dropdown {
public dropdownTypes = DropdownType;
@Input() public set dropdownType(value: any) {console.log(`-- dropdownType: ${value}`);};
constructor() {console.log('-- Dropdown ready --');}
}
@Component({ selector: 'header' })
@View({ template: '<div class="Dropdown" dropdownType="dropdownTypes.instrument"> </div>', directives: [Dropdown] })
class Header {}
@Component({ selector: 'my-app' })
@View({ template: '<header></header>', directives: [Header] })
class Tester {}
bootstrap(Tester);
2
比下面两个答案都好,尽管相似但比公认的答案简单,是:stackoverflow.com/a/42464835/358578
—
pbarranis