I'm using @HostListener("document:keyup", ["$event"]) to have a real Time operation. Let's say I have 3 inputs: A B and R(result). I want to sum A+B and show R in its input. But I'd like to modify R and get B modified by R-A.
On screen:
Input A: 5
Input B: 3
Input R: 8
Then, modify R manually to 5 change the value of B.
Input R: 8 -> 5. Input B should turn 0. But it never turns 0 because as soon as I press anything the function just calculates A+B and gives the result on R. How can I achieve this without creating a loop?
@HostListener("document:keyup", ["$event"])
public operar(){
this.gananciaEuros = 0;
this.gananciaPorcentaje ="";
this.resultado = "";
this.calc.controls.total.setValue(0);
if (!this.calc.controls.costo.value || !this.calc.controls.precioNeto.value) { //si la var está vacía
this.calc.controls.total.setValue("Rellene ambos campos");
}else{
this.resultado = Number(Number(this.calc.controls.precioNeto.value) / Number(this.calc.controls.costo.value));
this.gananciaEuros = Number((Number(this.calc.controls.precioNeto.value) - Number(this.calc.controls.costo.value)).toFixed(2));
this.gananciaPorcentaje = String(((this.resultado - 1) * 100).toFixed(2));
this.calc.controls.total.setValue(Number(this.gananciaEuros)/* +" (" + String(this.gananciaPorcentaje) +"%)" */);
this.calc.controls.precioNeto.setValue(Number(this.gananciaEuros) + Number(this.calc.controls.costo.value))
}
}
html
<div class="divformulario">
<form [formGroup]="calc">
<div class="formsubconjuntovertical spacebetween">
<div [innerHtml]="'Calcular Ganancia sobre precio' | uppercase | bold" "></div>
<mat-form-field>
<mat-label>Precio costo</mat-label>
<input matInput type="number" formControlName="costo" />
</mat-form-field>
<mat-form-field >
<mat-label>Precio de venta neto</mat-label>
<input matInput type="number" formControlName="precioNeto" " />
</mat-form-field>
<mat-form-field>
<mat-label>Ganancia</mat-label>
<input matInput type="number" formControlName="total" style="text-align: right;" "/><span matSuffix>€</span>
</mat-form-field>
</div>
</form>
</div>