douglas
douglas

Reputation: 121

how can I pass the value of a select with disabled in angular?

Hello I am using a select disabled to pass the value to an input but I can't find the way before without the disabled I used this:

 private subscribeToForm = () => {
    this.form.get('idLinea').valueChanges.subscribe(value => {
      console.log(value)
      this.handleIdLineaChange(value);    
    });   }

and here I used the value to show it in the input

private handleIdLineaChange = (value) => {
    if (value) {
      const idTipoLinea = this.form.get('idLinea').value;
      console.log(idTipoLinea)
      const fechaEvento = this.form.get('fechaEvento').value;

      let fecha = new Date(fechaEvento);

      this.presupuestoService.getPresupuesto(idTipoLinea, fecha.getFullYear()).subscribe((resp) => {
        const pto = resp.data[0];
        this.form.patchValue({
          presupuestoAsignadoLinea: pto.ptoAsignado,
          presupuestoUsado: pto.ptoUsado,
          presupuestoDisponible: pto.ptoDisponible
        });
      }, () => {
        this.toastService.warning(MESSAGES.NO_EXISTE_PRESUPUESTO);
      });
    } else {
      this.form.patchValue({
        presupuestoAsignadoLinea: null,
        presupuestoUsado: null,
        presupuestoDisponible: null
      });
    }
  }

html

<div class="col-md-2">
    <lib-form-control isRequired
                      label="Línea"
                      variant="select"
                      placeholder
                      controlName="idLinea"
                      bindValue="idParam"
                      bindLabel="valor"
                      [formGroupParent]="form"
                      [options]="state.comboLists.linea.list"
                      [disabled]="true"
                      >
    </lib-form-control>   </div>

enter image description here

Upvotes: 1

Views: 338

Answers (1)

Eliseo
Eliseo

Reputation: 57919

this.form.get('idLinea').value should work anycase

I think that your problem is that the valueChanges not executed. I think you need usestartWith rxjs/operator in valueChanges to execute the change at first

this.form
  .get('idLinea')
  .valueChanges.pipe(
           startWith(this.form.get('idLinea').value))
  .subscribe(value=> {
    this.handleIdLineaChange(value);    
  });

See this fool stackblitz

Upvotes: 1

Related Questions