i'm trying to delete a row from a table. But i got an error that said TypeError: Cannot read property 'value' of undefined when i put the delete button at the end of a row. I followed this video link , but i just need to make the delete button functional without load row data to the form.
//service.ts
deleteProduct(key: string) {
this.productList.remove(key);
}
//component.ts
onDelete(form: NgForm) {
//fungsi deleteProduct()
if (confirm('Are you sure to delete this record ?') == true) {
this.ProductService.deleteProduct(form.value.$prdKey);
//this.resetForm(form);
}
}
onItemClick(prd: Product) {
this.ProductService.selectedProduct = Object.assign({}, prd);
}
<!--the form-->
<form #productForm="ngForm" (ngSubmit)="onSubmit(productForm)">...</form>
<tr *ngFor="let product of productList">
<td>{{product.prdName}}</td>
<td><button type="button" class="btn btn-warning" (click)="onItemClick(product)" title="click to edit or delete" data-toggle="modal" data-target="#myModal">Update</button></td>
<td><button type="button" class="btn btn-danger" *ngIf="ProductService.selectedProduct.$prdKey == null" (click)="onDelete(ProductService.selectedProduct.$prdKey)">Delete</button></td>
</tr>
The video tutorial version actually is *ngIf="ProductService.selectedProduct.$prdKey != null". I made the *ngIf="ProductService.selectedProduct.$prdKey == null" so the delete button will appear at the end of the row without clicking a row first. Can anyone help me solve this? Please let me know if more snippets are needed. Thank you in advance.