角度增量或减量计数

时间:2018-03-23 14:08:27

标签: angular increment decrement

我正在构建一个Angular 5应用程序来跟踪利润。 我的应用程序有多个输入字段。

如果我卖巧克力,那么我会按+按钮增加计数。

如果我卖Wine,那么我会按下Wine输入旁边的+按钮来增加计数。

现在我使用了incrementchoc()函数来增加巧克力的数量 和incrementwine()函数增加葡萄酒的数量。

以下是我的HTML:

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

我也附上了一张照片。 This is how my HTML looks like with the - and + buttons

以下是我的Component.ts文件的代码

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

从上面的代码可以清楚地看到,如果我有n个输入字段,那么我将不得不写入n个递增和递减函数。我认为这会使应用程序非常繁重。

请建议我通过使用单个函数增加递减值,通过检测输入的ngModel并仅递增/递减该值而不是写入n个函数来建议我这样做。

2 个答案:

答案 0 :(得分:2)

您可以直接在组件模板中执行递增和递减操作。您不需要使用组件代码。

例如:

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

完整代码:

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

答案 1 :(得分:0)

是的,目前的方法使它变得沉重。你可以这样做:

increment(value: string) {
  if(value === 'wine') {
      this.info.wine = ++this.info.wine;
    }else if (value === 'choc'){
      this.info.choco= ++this.info.choco;
    }else{
      this.info.gir = ++this.info.gir;
    }
}
<button type="button" class="btn btn-success btn-number"  (click)= "incrementwine(wine)" data-type="plus" data-field="quant[3]">
    <span class="glyphicon glyphicon-plus"></span>
 </button>

相同的减量
*我没有检查代码的逻辑。只是重构。