Reputation: 1204
I want to Increment/Decrement when the user presses the add button, then update the amount value accordingly. I've tried a couple of times, but unfortunately I was not successful!
Here is the code:
addToCart(){
this.amount = 1;
}
addItem(){
this.amount++;
console.log('plus is : '+this.amount++)
}
removeItem(){
this.amount--;
console.log('plus is : '+this.amount--)
}
HTML:
<div (click)="addToCart()">ADD</div>
<div (click)="removeItem()" class="btnSign">-</div>
<div>{{amount}}</div>
<div (click)="addItem()" class="btnSign">+</div>
Upvotes: 3
Views: 6672
Reputation: 2123
The mistake was in using ++
export class HelloWorld {
public amount:number;
addToCart(){
this.amount = 1;
}
addItem(){
this.amount=this.amount+1;
console.log('plus is : '+this.amount)
}
removeItem(){
this.amount=this.amount-1;
console.log('plus is : '+this.amount)
}
}
Upvotes: 1