我有这样的HTML代码。
<div *ngFor="let item of items">
<div class="card col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class=" col-xs-12 col-sm-12 col-md-12 col-lg-12 col-xl-12" style="display:flex">
<div class="my-2 mx-10 col-xs-8 col-sm-8 col-md-8 col-lg-8 col-xl-8">
<h5 class="item-heading">{{item.title}}</h5>
<p>{{item.description}}</p>
</div>
<div class="my-4 col-xs-2 col-sm-2 col-md-2 col-lg-2 col-xl-2">
<strong>€{{item.Price}} </strong>
</div>
<div class="mx-10 col-xs-2 col-sm-2 col-md-2 col-lg-2 col-xl-2 p-0 m-0 d-flex " >
<button class="Increment p-0 " (click)="Increment({{item.count}})" >+</button>
<button class="Decrement p-0 " (click)="Decrement({{item.count}})">-</button>
<!-- <div class="middle-part"><input class="quantity" class=" col-6 quantity" type="number"></div> -->
<input class=" col-5 py-3 quantity" type="number" value="{{item.count}}">
</div>
</div>
</div>
</div>
在我的ts中我有
import { Component, OnInit } from '@angular/core';
import { CartItemsList,cartItemsArray } from './CartItemsList';
@Component({
selector: 'app-cart-items-component',
templateUrl: './cart-items-component.component.html',
styleUrls: ['./cart-items-component.component.css']
})
export class CartItemsComponentComponent implements OnInit {
count:number=0;
items = cartItemsArray;
item = this.items[0];
constructor() { }
ngOnInit() {
}
Increment(item)
{
item.count +=1;
}
Decrement(item)
{
item.count -=1;
}
}
现在,当我执行此操作时,我将使所有计数值递增或递减。我需要存储每个项目的个人计数值。
在cartItemsList.ts中,代码如下。
export class CartItemsList
{
title:string;
Price:number;
description:string;
count:number;
}
export const cartItemsArray: CartItemsList[] = [
{ title: 'Boulette Speciale', Price: 150 , description:'A product' , count:0},
{ title: 'Campagne Supreme', Price: 220 , description:'B product' , count:0 },
{ title: 'Boulette Speciale', Price: 250 , description:'C product' , count:0 },
{ title: 'Campagne Supreme', Price: 300 , description:'D product' , count:0 }
];
答案 0 :(得分:1)
我的建议是将item
对象传递给该函数并更新该对象的计数。
<button class="Increment p-0 " (click)="Increment(item)" >+</button>
在.ts文件中
Increment(item)
{
item.count +=1;
}