在 Angular 8 中使用带有枚举的 Formbuilder [英] Use Formbuilder with Enum in Angular 8

查看:18
本文介绍了在 Angular 8 中使用带有枚举的 Formbuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将 Enum 与 Formbuilder 名称联系起来?下面,是一个枚举,并且想要将枚举转换为它的字符串,并与 Formbuilder 一起使用(而不是在 formbuilder 中使用字符串)

Is there a way to tie an Enum to Formbuilder names? Below, is an enum, and want to convert enum to its string, and use with Formbuilder (rather than using strings in formbuilder)

这在 Angular 中可行吗?

Is this possible in Angular?

enum address{   
    city, 
    state, 
    zip
}

this.addressForm = this.formBuilder.group({
  'city': [null, [Validators.required, Validators.maxLength(200)]],
  'state': [null, [Validators.maxLength(200)]],
  'zip':[null,[Validators.maxLength(200)]]
});

推荐答案

是否有使用枚举的特定原因?

Is there a specific reason to use an enum?

我认为界面更符合您的要求:

I think an interface is more what you want:

interface address {
    city: string;
    state: string;
    zip: string;
}

如果你走接口路线,你可以在这里看看我的回答 参考 Angular 8 中强类型列表中的 FormBuilder 成员.它应该适用于您想要完成的工作.

If you go the interface route, you could take a look at my answer over here Refer to FormBuilder Members in a Strongly Typed list in Angular 8. It should apply to what you're trying to accomplish.

这篇关于在 Angular 8 中使用带有枚举的 Formbuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆