【发布时间】:2020-06-11 11:05:10
【问题描述】:
我有一个模板驱动的角度形式, 当我单击产品时,我想在其上填充值。 'description' 和 'imageUrl' 字段可能是未定义的,如果我不照顾它会破坏我的表单。 我设法用这个解决方案做到了:
private initFormEdit() {
this.product = this.productService.fetchProduct(this.restaurantId, this.productId);
// I'm using a ternaty operator to put a default value if objects are empty
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
});
}
有没有更简单或更干净的方法来做到这一点? 谢谢!
【问题讨论】:
标签: typescript angular-template-form