本文介绍了在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;
}

如果您选择接口路线,则可以在这里.它应该适用于您要完成的任务.

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与Enum一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 20:54