Angular Reagent Forms, valueChanges does not capture the new value

0

I have a problem that I've been trying to solve for some time.

I have 3 inputs that interact with each other to calculate the price with VAT and vice versa.

I leave you a stackblitz so you can see it better. Stackblitz

The problem is, when I write in the precioCompra the [value] of precioCompraIva should change and it does not ... That is, if it does, but I can not capture it.

Can someone tell me how to fix it? or why does it happen?

Thanks in advance

In the constructor I am subscribed to valueChanges, to control the values.

As you can see, when writing in precioCompra . The value of precioCompraIva does not change and vice versa neither.

    
asked by Sergio Cano 23.07.2018 в 02:00
source

1 answer

0

What happens is that you are making the changes in the form in the DOM. For the change detector to work well, you have to make the changes to the Form object that you created.

I enclose your example edited in Stackblitz .

What I'm doing is that you no longer enter the value in the HTML, but from there we take the events of (input) to detect the moment in which the user has entered some character in the form.

That event triggers the corresponding calculation functions, and the change is made in the Form object using the setValue method.

this.datosFormulario.get('preciocompra')
  .setValue(this.calcularPrecioCompraIva());

By using setValue we make sure that the change detector fires.

    
answered by 23.07.2018 / 02:29
source