我需要将enum作为参数传递给angular 6指令,但它可以识别为字符串
export enum MyEnum {
SomeValue = 1
}
指令代码:
import { MyEnum } from '/path/';
@Directive({
selector: '[appMyDir]'
})
export class MyDirDirective {
constructor() { }
private _role: MyEnum;
@Input()
public set role(val: MyEnum) {
this._role = val; // string
}
指令的用法:
<button appMyDir role=MyEnum.SomeValue >Button</button>
答案 0 :(得分:0)
您只能绑定到组件类的属性。将枚举设置为类属性之一,并将其绑定到指令。
import { MyEnum } from '/path/';
@Component({
template: `
<button appMyDir [role]="myEnum.SomeValue">Button</button>
`
})
export class AppComponent {
myEnum = MyEnum;
}