I am working with a Template driven form in Angular where I need to populate values upon clicking on a product. The 'description' and 'imageUrl' fields may sometimes be undefined, which can cause issues with my form if not handled properly. My current solution involves using a ternary operator:
private initFormEdit() {
this.product = this.productService.fetchProduct(this.restaurantId, this.productId);
// Using a ternary operator to set default values for empty objects
const description = this.product.description ? this.product.description : '';
const imageUrl = this.product.imageUrl ? this.product.imageUrl : '';
this.productForm.setValue({
name: this.product.name,
category: this.product.category,
description,
price: this.product.price,
imageUrl
});
}
Are there any more efficient or cleaner alternatives to achieve the same result? Appreciate any suggestions!