[英]ERROR TypeError: Cannot read property 'name' of undefined when i try to add product
I try to make a basket as simply as possible, I try to add a product to my basket but I have an error but I cannot find my error.我尝试尽可能简单地制作一个篮子,我尝试将产品添加到我的篮子中,但我有一个错误,但我找不到我的错误。
thank you谢谢你
html html
<h2>Product</h2>
<div class="card" *ngFor="let product of productList">
<h1>{{product.name}}</h1>
<p class="price">{{product.price | currency: 'USD'}}</p>
<p><button (click)=add()>Add to Cart</button></p>
</div>
</div>
<div>
<h2>Total</h2>
<div class="card">
<table>
<thead>
<tr>
<th>product</th>
<th>price</th>
<th>Quantity</th>
<th>total</th>
</tr>
</thead>
<tbody *ngFor="let added of productArray">
<tr>
<td>{{added.name}}</td>
<td>{{added.price}}</td>
<td>x 10</td>
<td>45€</td>
ts.file ts.文件
productList = [
{ name: 'Louis Vuis', price: 10 },
{ name: 'shubert helmet', price: 20 },
{ name: 'sport gloves', price: 30 }
];
productArray: any = [];
add(product) {
this.productArray.push(product);
}
In your template, you have to change (click)="add()"
to (click)="add(product)"
在您的模板中,您必须将
(click)="add()"
更改为(click)="add(product)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.