我们可以在angular2视图模板中使用枚举吗?
<div class="Dropdown" dropdownType="instrument"></div>
Run Code Online (Sandbox Code Playgroud)
传递字符串作为输入:
enum DropdownType {
instrument,
account,
currency
}
@Component({
selector: '[.Dropdown]',
})
export class Dropdown {
@Input() public set dropdownType(value: any) {
console.log(value);
};
}
Run Code Online (Sandbox Code Playgroud)
但是如何传递枚举配置?我在模板中想要这样的东西:
<div class="Dropdown" dropdownType="DropdownType.instrument"></div>
Run Code Online (Sandbox Code Playgroud)
什么是最佳做法?
编辑:创建一个例子:
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() …
Run Code Online (Sandbox Code Playgroud) 我有几个类我想成为一个普通的bean/DTO类,它们不是显示@component类,它们不是@Pipe类,也不是@Directive(至少我不认为它应该是!).
我希望能够将它们捆绑到一个模块中(它们将用于其他模块),但是尽管有几个咒语,我仍然会遇到这样的错误:
当我启动我的Angular应用程序(ng服务)时,它编译正常,但在浏览器(Chrome)控制台中我收到错误....
Uncaught Error: Unexpected value 'Accreditation' declared by the module 'ServiceModule'. Please add a @Pipe/@Directive/@Component annotation.
Run Code Online (Sandbox Code Playgroud)
我应该如何将这些类捆绑到一个模块中供其他模块使用?我不介意他们是在我的服务模块中还是在另一个新的'beans'模块中.
给定此模块定义(service.module.ts):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {MemberService} from "./member/member.service";
import { Accreditation } from "./accreditation";
import {Player} from "./player";
import {Club} from "./club";
import {Coach} from "./coach";
@NgModule({
imports: [CommonModule],
declarations: [Accreditation, Club, Player, Coach],
exports: [Accreditation, Club, Player, Coach],
providers: [MemberService]
})
export class ServiceModule { }
Run Code Online (Sandbox Code Playgroud)
accreditation.ts:
export class Accreditation { …
Run Code Online (Sandbox Code Playgroud)