首页 > 解决方案 > 如何在角度禁用的情况下传递选择的值?

问题描述

您好,我正在使用选择禁用将值传递给输入,但是在没有禁用之前我找不到方法我使用了这个:

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

在这里我使用该值在输入中显示它

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>

在此处输入图像描述

标签: angulartypescript

解决方案


this.form.get('idLinea').value 无论如何都应该工作

我认为您的问题是 valueChanges 未执行。我认为您首先需要startWith在 valueChanges 中使用 rxjs/operator 来执行更改

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

看看这个傻瓜stackblitz


推荐阅读