Hello, I am working on adding an input value to an object property.
The scenario is that a customer wants to add an item to their shopping cart, but before adding the item, they need to choose the size. I have the form set up with validation and can retrieve the size value when a radio button is clicked and the onSubmit() function is called. The cart service function is already in place for adding items to the cart using an API, and I can retrieve the id of the posted item. Now, I need to get the Valuesize and add it as a property (size:Valuesize), which is a string.
I pass in Valuesize as a parameter to the cartservice. My challenge lies in updating the object property. I tried implementing this functionality, but it's not behaving as expected. Any guidance on how to proceed would be greatly appreciated.
Code snippet below:
Model
My model has 'size' as an optional property, so it doesn't exist on the object by default. I'm not sure if I should make a post request first, followed by a get request by id to add the property afterwards.
import { Product } from './product';
export class CartItem {
static splice(arg0: number) {
throw new Error('Method not implemented.');
}
id: number;
productId: number;
productName: string;
qty: number;
price: number;
size?:string;
imageUrl:string;
constructor(id:number, size:string, product:Product, qty=1) {
this.id = id;
this.productId = product.id;
this.price = product.price;
this.size = size;
this.productName = product.name;
this.qty = qty;
this.imageUrl = product.imageUrl;
}
}
productlist.ts
// Component code here...
cartservice
// Service code here...
The JSON structure in db.json
"cart": [
{
"product": {
"id": 4,
"name": "Purple Outfit",
"description": "Lorem Ipsum is simply dummy text...",
"imageUrl":"http://localhost:4200/assets/purpleoutfit4.png",
"price": 100
},
"id": 7
}
],