Angular increment or decrement count

node_man picture node_man · Mar 23, 2018 · Viewed 18.7k times · Source

I'm building an Angular 5 application to track profit. My application has multiple input fields.

If I sell a Chocolate then I'll press the + button to increase the count.

If I sell Wine then I'll press the + button next to Wine input to increase the count.

Now I have used incrementchoc() function to increase the count of chocolate and incrementwine() function to increase the count of wine.

Here's my HTML below :

 <div >
                <label>Chocolate:</label><br>
            <div class="input-group">
              <span class="input-group-btn">
                  <button type="button" class="btn btn-danger btn-number" (click)= "decrementchoc()" data-type="minus" data-field="quant[2]">
                    <span class="glyphicon glyphicon-minus"></span>
                  </button>
              </span>

              <input type="text" name="quant[2]" [(ngModel)]="info.choco" (keyup)="choc(info.choco)" class="form-control input-number" value="{{info.choco}}" >

              <span class="input-group-btn">
                  <button type="button" class="btn btn-success btn-number"  (click)= "incrementchoc()" data-type="plus" data-field="quant[2]">
                      <span class="glyphicon glyphicon-plus"></span>
                  </button>
              </span>
          </div></div>



<div >
                    <label>Wine:</label><br>
                <div class="input-group">
                  <span class="input-group-btn">
                      <button type="button" class="btn btn-danger btn-number" (click)= "decrementwine()" data-type="minus" data-field="quant[3]">
                        <span class="glyphicon glyphicon-minus"></span>
                      </button>
                  </span>

                  <input type="text" name="quant[3]" [(ngModel)]="info.wine" (keyup)="wine(info.wine)" class="form-control input-number" value="{{info.wine}}" >

                  <span class="input-group-btn">
                      <button type="button" class="btn btn-success btn-number"  (click)= "incrementwine()" data-type="plus" data-field="quant[3]">
                          <span class="glyphicon glyphicon-plus"></span>
                      </button>
                  </span>
              </div></div>

I'm attaching an image as well. This is how my HTML looks like with the - and + buttons

Below is the code for my Component.ts file

 incrementchoc(){
    this.info.choco= ++this.info.choco;
  }

  decrementchoc(){
    this.info.choco= --this.info.choco;
  }


   incrementwine(){
    this.info.wine= ++this.info.wine;
  }

  decrementwine(){
    this.info.wine= --this.info.wine;
  }

You can clearly see from the above code that if i have n number of input fields then I'll have to write n number of increment and decrement functions. I think this will make the application very heavy.

Please suggest me a better way of doing this by using a single function to increment decrement the values by detecting the ngModel of an input and incrementing/decrementing only that value rather than writing n number of functions.

Answer

Mike Hill picture Mike Hill · Mar 23, 2018

You can do the increment and decrement operations in the component template directly. You don't need to use component code for those.

For example:

<button type="button" class="btn btn-danger btn-number"
        (click)="info.choco = info.choco - 1" data-type="minus" data-field="quant[2]">
    <span class="glyphicon glyphicon-minus"></span>
</button>

Full code:

<div>
    <label>Chocolate:</label><br>
    <div class="input-group">
        <span class="input-group-btn">
            <button type="button" class="btn btn-danger btn-number"
                    (click)="info.choco = info.choco - 1" data-type="minus" data-field="quant[2]">
                <span class="glyphicon glyphicon-minus"></span>
            </button>
        </span>

        <input type="text" name="quant[2]" class="form-control input-number"
                [(ngModel)]="info.choco" (keyup)="choc(info.choco)" value="{{info.choco}}" >

        <span class="input-group-btn">
            <button type="button" class="btn btn-success btn-number"
                    (click)="info.choco = info.choco + 1" data-type="plus" data-field="quant[2]">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
        </span>
    </div>
</div>

<div>
    <label>Wine:</label><br>
    <div class="input-group">
        <span class="input-group-btn">
            <button type="button" class="btn btn-danger btn-number"
                    (click)="info.wine = info.wine - 1" data-type="minus" data-field="quant[3]">
                <span class="glyphicon glyphicon-minus"></span>
            </button>
        </span>

        <input type="text" name="quant[3]" class="form-control input-number"
                [(ngModel)]="info.wine" (keyup)="wine(info.wine)" value="{{info.wine}}">

        <span class="input-group-btn">
            <button type="button" class="btn btn-success btn-number"
                    (click)="info.wine = info.wine + 1" data-type="plus" data-field="quant[3]">
                <span class="glyphicon glyphicon-plus"></span>
            </button>
        </span>
    </div>
</div>