【发布时间】:2019-05-10 04:08:30
【问题描述】:
我想实现选择菜单,它使用枚举数据来显示数据和基于所选字符串保存的数字:
HTML 代码:
<div class="form-group state_raw">
<label for="state_raw">State</label>
<select class="custom-select" name="state_raw" [(ngModel)]="merchant.state_raw" id="state_raw" required>
<option selected></option>
<option [value]="type" *ngFor="let type of types">{{type | formatType}}</option>
</select>
</div>
显示数据和翻译数值的枚举:
export enum MerchantStatusType {
'Being set up' = 1,
'Live for processing' = 2,
'Trading suspended' = 3,
'Account suspended' = 4
}
选择菜单的对象:
export class MerchantNew {
constructor(
public name: string,
public address_state: string,
) {}
}
如何实现?我想显示字符串但将数字保存到数据库中?
编辑:我试过这个:
枚举:
export enum MerchantStateType {
being_set_up = 1,
live_for_processing = 2,
trading_suspended = 3,
account_suspended = 4,
}
export const MerchantStateType2LabelMapping = {
[MerchantStateType.being_set_up]: "Being set up",
[MerchantStateType.live_for_processing]: "Live for processing",
[MerchantStateType.trading_suspended]: "Trading suspended",
[MerchantStateType.account_suspended]: "Account suspended",
}
组件:
public MerchantStateType2LabelMapping = MerchantStateType2LabelMapping;
public stateTypes = Object.values(MerchantStateType);
HTML 代码:
<select class="custom-select" name="state_raw" [(ngModel)]="merchant.state_raw" id="state_raw" required>
<!--<option selected></option>-->
<option [value]="stateType" *ngFor="let stateType of stateTypes">{{ MerchantStateType2LabelMapping[stateType] }}</option>
但我得到了 4 个空行和 4 个状态行。
【问题讨论】:
-
我不知道哪个部分阻止了你。看来你几乎完成了。请查看文档中的示例:angular.io/guide/dynamic-form#question-model。它可能会对你有所帮助。
-
你能告诉我如何在html代码中使用枚举吗?
-
@PeterPenzov:你能告诉我
merchant和types是什么吗?
标签: angular typescript angular6