gpt4 book ai didi

enums - 在 angular2 View 模板中传递枚举

转载 作者:太空狗 更新时间:2023-10-29 16:45:24 26 4
gpt4 key购买 nike

我们可以在 angular2 View 模板中使用枚举吗?

<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);

最佳答案

创建一个枚举:

enum ACTIVE_OPTIONS {
HOME = 0,
USERS = 1,
PLAYERS = 2
}

创建一个组件,确保您的枚举列表具有typeof:

export class AppComponent {
ACTIVE_OPTIONS = ACTIVE_OPTIONS;
active: ACTIVE_OPTIONS;
}

创建模板:

<li [ngClass]="{ 'active': active === ACTIVE_OPTIONS.HOME }">
<a routerLink="/in">
<i class="fa fa-fw fa-dashboard"></i> Home
</a>
</li>

关于enums - 在 angular2 View 模板中传递枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35923744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com