Use Formbuilder with Enum in Angular 8

你。 提交于 2020-01-17 15:43:53

问题


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)

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

回答1:


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.



来源:https://stackoverflow.com/questions/59742095/use-formbuilder-with-enum-in-angular-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!