问题
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