当我使用双向绑定以进行数量时,如何避免更改另一个文本框?
我在垫子卡上使用 *ngfor,因此它将从我的书籍表中检索所有数据。我还在卡上添加了一个文本框,以便用户可以输入数量。我在文本框上使用了双向绑定。我的问题是,当我更改文本框上的数量时,所有文本框也会更改。我如何避免这种情况发生?请参阅下图:
这是我的代码:
store.component.html
<div class="card-container">
<mat-card *ngFor="let card of obs | async; let i = index" class="mt-3">
<img mat-card-image src="{{card.bookimage}}" width="100" height="200">
<mat-card-header>
<mat-card-title>{{card.bookname}}</mat-card-title>
<mat-card-subtitle>₱{{card.bookprice.toFixed(2)}}<span *ngIf="card.bookunitstock === 0" class="status text-white bg-danger mx-2">Out of Stock</span></mat-card-subtitle>
</mat-card-header>
<mat-card-actions>
<input type="number" [(ngModel)]="quantity" name="quantity">
<button mat-raised-button color="primary" (click)="onAddToCartProduct(card)"><mat-icon class="me-2">add_shopping_cart</mat-icon>Add to cart</button>
</mat-card-actions>
</mat-card>
</div>
store.component.ts
public onAddToCartProduct(book: Book): void {
const formValue = {
bookid: book.bookid,
bookimage: book.bookimage,
bookname: book.bookname,
bookprice: book.bookprice,
bookstatus: book.bookstatus,
cartitemid: 0,
category: "Fantasy",
checkoutstatus: true,
isbn: book.isbn,
quantity: this.quantity,
sku: book.sku,
totalprice: book.bookprice * this.quantity,
user_userid: 4
}
console.log(formValue);
this.storeService.addCartProduct(formValue).subscribe(
(response: Store) => {
this.products.push(formValue);
this.grandTotal += (formValue.quantity * formValue.bookprice);
this.successMessage("added");
},
(error: HttpErrorResponse) => {
this.errorMessage("Out -of Stock");
}
);
}
I'm using *ngFor on my mat-card so it will retrieve all the data from my book table. I also add a textbox on my card so the user can input the quantity. I used the two-way binding on my textbox. My problem is when I changed the quantity on the textbox, all the textboxes are also changed. How can I avoid this to happen? See the picture below:
Here's my code:
store.component.html
<div class="card-container">
<mat-card *ngFor="let card of obs | async; let i = index" class="mt-3">
<img mat-card-image src="{{card.bookimage}}" width="100" height="200">
<mat-card-header>
<mat-card-title>{{card.bookname}}</mat-card-title>
<mat-card-subtitle>₱{{card.bookprice.toFixed(2)}}<span *ngIf="card.bookunitstock === 0" class="status text-white bg-danger mx-2">Out of Stock</span></mat-card-subtitle>
</mat-card-header>
<mat-card-actions>
<input type="number" [(ngModel)]="quantity" name="quantity">
<button mat-raised-button color="primary" (click)="onAddToCartProduct(card)"><mat-icon class="me-2">add_shopping_cart</mat-icon>Add to cart</button>
</mat-card-actions>
</mat-card>
</div>
store.component.ts
public onAddToCartProduct(book: Book): void {
const formValue = {
bookid: book.bookid,
bookimage: book.bookimage,
bookname: book.bookname,
bookprice: book.bookprice,
bookstatus: book.bookstatus,
cartitemid: 0,
category: "Fantasy",
checkoutstatus: true,
isbn: book.isbn,
quantity: this.quantity,
sku: book.sku,
totalprice: book.bookprice * this.quantity,
user_userid: 4
}
console.log(formValue);
this.storeService.addCartProduct(formValue).subscribe(
(response: Store) => {
this.products.push(formValue);
this.grandTotal += (formValue.quantity * formValue.bookprice);
this.successMessage("added");
},
(error: HttpErrorResponse) => {
this.errorMessage("Out -of Stock");
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
之所以发生,是因为属性
this.quantity
在所有卡上使用让obs
的卡 - 它们都引用相同的道具。取而代之的是,您可以用
[(ngmodel)] =“ card?Quantity”
在每个单独的卡上绑定到prop 。并在
formValue
相应地更改数量:book.quantity
。在命名OBJ时尝试保持一致,那么它是
book
还是card
?祝你好运!其他选项是使用模板变量。我们从参考
#MyInput
中获取输入
值,然后将其传递给方法onaddtocartproduct()
。It happens because property
this.quantity
is used on all cardslet card of obs
- they all reference the same prop.Instead you could bind to prop
quantity
on each individual card with[(ngModel)]="card?.quantity"
.And on
formValue
change accordinglyquantity: book.quantity
.Try to stay consistent when naming obj's, so is it a
book
or acard
? Good luck!Other option would be to use template variable. We take
input
value from reference#myInput
and pass it to methodonAddToCartProduct()
.