The goal: Create a booking instance via the Angular frontend
What I've done: I'm setting one of the booking fields "category" with an HTML option input via TypeScript. The field has an object as data type. That object has two fields, id and name.
When I set the category field with the value I get from the option input, it sets the category name with that value, but not the id. How can I set the id field instead of the name field?
Screenshot of debugging:
booking.ts
import {Category} from "./category";
export interface Booking {
id: number;
value: number;
category: Category;
date: Date;
}
category.ts
export interface Category {
id: number;
name: string;
}
option input:
<select type="text" ngModel name="category" class="custom-select" id="category">
<option selected>wähle...</option>
<option *ngFor="let category of categories" value="{{category.id}}">{{category.name}}
</option>
</select>
Create Booking function:
public createBooking(addBookingForm: NgForm) {
this.bookingService.addBooking(addBookingForm.value).subscribe(
(response: Booking) => {
console.log(response);
window.location.reload();
},
(error: HttpErrorResponse) => {
alert(error.message);
}
)
}