i need to pass enum type
to server . but when i send the form to server it send enum fild
by type string
.
this is my enum :
export enum PayTypes {
Free,
Subscribe
}
and this is my form :
createForm(): void {
this.lessonAddForm = new FormGroup({
payType: new FormControl('', Validators.required),
});
}
addLesson(): void {
const payString = PayTypes[this.lessonAddForm.controls['payType'].value];
const payType: PayTypes = PayTypes[payString];
this.lessonAdd = this.lessonAddForm.value;
this.lessonAdd.payType = payType;
this.subscriptions = this.lessonService
.add(this.lessonAdd)
.subscribe(
res => {
if (res.success === true) {
this.alertService.success('', 'GENERAL.ADD_SUCCESS');
this.backToMenu();
}
}
);
}
and i try for convert string to enum by this way :
const payString = PayTypes[this.lessonAddForm.controls['payType'].value];
const payType: PayTypes = PayTypes[payString];
this.lessonAdd = this.lessonAddForm.value;
this.lessonAdd.payType = payType;
but it not work .
How can i solve this Problem????
You must convert the value inside the square bracket to string like this
const payType: PayTypes = PayTypes[payString.toString()];
Also you can view this post
Update: Check how you get payString data does it match with your enum definition ? Please note your payString data have to be the same with your enum key
For example:
PayTypes['Free'] // This will work
And
PayTypes['free'] // This won't
Why dont you just assign values to the enum keys like below
export enum PayTypes {
Free = 'Free',
Subscribe = 'Subscribe'
}
You can read more about it Here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.