Folky.H
Folky.H

Reputation: 1204

Angular2/JavaScript - Increment/Decrement by 1 on (click)

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

Answers (1)

KB_
KB_

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)
}


}

here is the plunker

Upvotes: 1

Related Questions