【问题标题】:value of formControl not displayed on viewformControl 的值未显示在视图上
【发布时间】:2020-10-09 16:11:13
【问题描述】:

我有一个包含许多字段的表单,它是由 formGroup 和 formControlName 组成的。 问题是当我启动应用程序时,我需要从后端获取数据并将其显示在视图上。

问题是Funcion这个字段没有显示值:

这是我的 html 代码的精简部分:

<form #f="ngForm" [formGroup]="registros" class="fila-contenido">
                <div class="campos">
                    <!-- En Venta THIS IS OK-->
                    <label>¿En venta?</label>
                    <mat-radio-group formControlName="enVenta" aria-labelledby="example-radio-group-label"
                        class="example-radio-group">
                        <mat-radio-button value="1" class="example-radio-button">Si</mat-radio-button>
                        <mat-radio-button value="0" class="example-radio-button">No</mat-radio-button>
                    </mat-radio-group>
                </div>

                <!-- Activo THIS IS OK-->
                <div class="campos">
                    <label>¿Registrado?</label>
                    <mat-radio-group formControlName="activo" aria-labelledby="example-radio-group-label"
                        class="example-radio-group">
                        <mat-radio-button value="Si" class="example-radio-button">Si</mat-radio-button>
                        <mat-radio-button value="En Trámite" class="example-radio-button">En Trámite</mat-radio-button>
                        <mat-radio-button value="No Necesita" class="example-radio-button">No Necesita
                        </mat-radio-button>
                        <mat-radio-button value="No" class="example-radio-button">No</mat-radio-button>
                    </mat-radio-group>
                </div>

                <!-- Pais THIS IS OK-->
                <mat-form-field appearance="">
                    <mat-label>Pais</mat-label>
                    <mat-select formControlName="pais">
                        <mat-option *ngFor="let pais of selectedPaises" [value]="pais">{{pais}}</mat-option>
                    </mat-select>
                </mat-form-field>

                <!-- Funcion THIS IS NOT OK-->
                <mat-form-field appearance="">
                    <mat-label style=" font-weight: bold;">Función</mat-label>
                    <mat-select formControlName="funcion" required (selectionChange)=changeFuncion($event)>
                        <mat-option *ngFor="let funcion of funciones" [value]="funcion">{{funcion.Funcion}}
                        </mat-option>
                    </mat-select>
                </mat-form-field>
</form>

这里是 TS 代码:

ngOnInit(): void {

    this.getRegistros();


  }

getRegistros() {
    this.http.get<Registro>('http://localhost:7000/api/registros/' + this.service.getRow()).subscribe(data => {
      data.log = data.log.replace(/\\"/g, '"');
      console.log("formateo", JSON.parse(data.log));
      let convert = JSON.parse(data.log);

      this.registros.controls.enVenta.setValue(convert.enVenta); //this is OK
      this.registros.controls.activo.setValue(convert.activo); //this is OK
      this.registros.controls.pais.setValue(convert.pais); //this is OK
      this.registros.controls.funcion.setValue(convert.funcion); //this is not OK

});
}

我尝试过的: 我写了一个包含表单所有值的 console.log,表单包含所有值,包括未显示的值。

我认为这可能与 ngOnInit 问题有关。但是我尝试将getRegistros() 放在ngAfterViewInit 上,我遇到了同样的问题:( 我也尝试使用patchValue() 而不是setValue() 但同样的问题

你有什么建议吗?

谢谢。

【问题讨论】:

    标签: javascript angular typescript angular-reactive-forms angular-forms


    【解决方案1】:

    我认为问题出在您的模板文件中:

    更新模板:

    <mat-form-field appearance="">
                        <mat-label style=" font-weight: bold;">Función</mat-label>
                        <mat-select formControlName="funcion" required (selectionChange)=changeFuncion(funcion) [(ngModel)]="funcion">
                            <mat-option *ngFor="let funcion of funciones" [value]="funcion">{{funcion.Funcion}}
                            </mat-option>
                        </mat-select>
                    </mat-form-field>
    

    组件:

    funcion:any;
    

    【讨论】:

    • 感谢您的回答,但不建议将 ngModel 添加到表单字段。如果您查看我的 html 代码,您会发现我有另一个名为“pais”的“选择”,它没有 ngModel,它正在检索数据。
    • 是的,没有 ngModel 也可以工作,我可以知道你为什么在这里使用 {{funcion.Funcion}} 吗?
    • 嗨,因为对象有 2 个不同的字段,但我只需要显示 1..
    • 好的,您能否在 stackblitz 上创建一个可重现的演示,如果您使用的是 reactiveForms 方法,则不应使用 #f="ngForm"。
    【解决方案2】:

    Angular 使用对象标识来选择选项。这是可能的 项目的身份要更改,而数据不会更改。这个可以 例如,如果项目是从 RPC 生产到 服务器,然后重新运行该 RPC。即使数据没有变化, 第二个响应将产生具有不同身份的对象。

    为了克服这个问题,我们必须提供 compareFn 函数来使用 compareWith 输入来告诉 Angular 如何比较值。

    试试这个:

    component.html

       <mat-form-field appearance="">
                    <mat-label style=" font-weight: bold;">Función</mat-label>
                    <mat-select formControlName="funcion" required (selectionChange)=changeFuncion($event) [compareWith]="compareWithFn">
                        <mat-option *ngFor="let funcion of funciones" [value]="funcion">{{funcion.Funcion}}
                        </mat-option>
                    </mat-select>
                </mat-form-field>
    

    component.ts

    这里我假设你的 funciones 对象包含 id 属性。

    compareWithFn(listOfItems,selectedItem){
         return listOfItems && selectedItem && listOfItems.id === selectedItem.id; ;
    }
    

    【讨论】:

    • 您好,感谢您的帮助。我是否必须将 CompareWithFn 添加到所有选择的 formFields 中?
    • 您好,非常感谢您的帮助。你救了我的命。我对 compareWithFn 做了一些修改,代码如下:compareWithFn(item1, item2) { return item1 &amp;&amp; item2 ? item1.nom === item2.nom : item1 === item2; }
    • 很高兴为您提供帮助?
    猜你喜欢
    • 2019-05-27
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多