How to add values in keyup using angular?

Asked

Viewed 106 times

0

I have a field that I need that as I type values, it adds in a variable.

I tried something like:

<input (keyup)="somaEstoque($event)"  #inputestoque type="text" name="estoque_variacao{{i}}" class="form-control" id="estoque_variacao{{i}}" formControlName="estoque_variacao">

  somaEstoque(valor){
    this.produto.estoque = this.produto.estoque + toFloat(valor.target.value);
  }

My target to be added is a formgroup that may own "N" stock inputs:

<div class="col-12 col-md-2">
    <div class="md-form form-lg input-modal">
       <input mask="000000000000" (keyup)="somaEstoque($event)"  #inputestoque type="text" name="estoque_variacao{{i}}" class="form-control" id="estoque_variacao{{i}}" formControlName="estoque_variacao">
       <label [class.active]="inputestoque.value != ''" for="estoque_variacao{{i}}">Estoque</label>
    </div>
</div>

I believe that logic is a little more complex, and also has to deal with when the user deletes the value.

Someone’s done it at the angle?

1 answer

0


If you’re interested, I got it that way:

  soma(){
    let sum = 0;
    Object.keys(this.controls).map(key => {
      sum +=  +this.controls[key];
    });
    this.produto.estoque = sum;
  }

 <input mask="000000000000" [(ngModel)]="controls[i]" (keyup)="soma($event)" #inputestoque type="text" name="estoque_variacao{{i}}" class="form-control" id="estoque_variacao{{i}}" formControlName="estoque_variacao">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.