jeudi 2 juillet 2020

Toggling value of variable assigned to checkbox in angular

I have a checkbox:

  <input type="checkbox" [checked]="passenger.luggage" (change)="passenger.luggage = ! passenger.luggage">Luggage

and I want to add +1 when it's checked to a variable assigned in components.ts:

public luggageCounter: number = 0;

I tried applying a function on change

luggageAdd() {
    this.luggageCounter++
  }

but it only adds +1 whenever the checkbox is clicked/changed. I'm looking for a solution where it adds +1 only when checked and if it's no longer checked it takes this +1 back.

Thanks for helping solve this!




Aucun commentaire:

Enregistrer un commentaire