Angular 5 + Angular Material Select + Reactive Forms == 没有显示初始选项 [英] Angular 5 + Angular Material Select + Reactive Forms == No initial options displayed

查看:32
本文介绍了Angular 5 + Angular Material Select + Reactive Forms == 没有显示初始选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我有一个响应式表单,其中包含多个 .在初始表单加载时,即使 form.value 显示了初始选项,也不会显示它.

As the title says, I have a reactive form that has multiple <mat-select> contained within. On initial form load, the initial option is not displayed even though form.value shows it is.

相关组件.ts:

export class DesJobInfoEditComponent implements OnInit {

...

currentJobData: IJob;
jobTypes: IJobType[];

...

constructor(private fb: FormBuilder) {

    ...

        // Construct forms
        this.createForm();

        this.initializeForm();

}

createForm() {
    this.editJobInfoForm = this.fb.group({
        ...
        JobType: '', // mat-select
        ...
    });
}

initializeForm() {
    this.rebuildForm();
}

rebuildForm() {
    this.editJobInfoForm.reset({
        ...
        JobType: this.jobTypes[this.currentJobData.JobType].DisplayDesc,
        ...
    });
}

}

相关的html:

<mat-form-field fxFlex>
      <mat-label>Job Type</mat-label>
       <mat-select formControlName="JobType" placeholder="Job Type">
              <mat-option *ngFor="let jobType of jobTypes" value="jobType.value">
                     {{ jobType.DisplayDesc }}
               </mat-option>
        </mat-select>
  </mat-form-field>

当表单加载时,选择不显示最初选择的选项,但是,它们显然设置正确:

When the form loads, the selects do not display the initially selected option, however, they are set properly, apparently:

Form value { ... "JobType": "0 - Standard", ... } 

表单上显示的只是占位符.

All that displays on the form is the placeholder.

这似乎不应该这么难.

我做错了什么?

this.jobTypes 在加载模块时加载,它是一个存在于我的数据服务中的 BehaviorSubject.我因此在此组件的构造函数中订阅它:

this.jobTypes is loaded when the module is loaded, and it is a BehaviorSubject that lives in my data service. I subscribe to it in the constructor of this component thusly:

this.data.jobTypes.subscribe(jobTypes => { this.jobTypes = jobTypes });

推荐答案

一些事情

  1. [formControlName] 必须[formGroup]结合使用.如果不想使用[formControlName] + [formGroup],可以使用[formControl]代替.

  1. [formControlName] must be used in conjunction with [formGroup]. If you don't want to use [formControlName] + [formGroup], you can use [formControl] instead.

在 angular 中,将属性指定为 value[value] 是有区别的.当一个属性被括在括号 [] 中时,它被解释为 javascript/ 角度模板脚本(与 {{}} 相同,我认为).当它not括在括号中时,它被解释为一个字符串(即value="jobType.value" === [value]="'jobType.value'"[value]="jobType.value" === value="{{jobType.value}}" (实际上我认为[value]="jobType.value"value="{{jobType.value}}" 之间存在细微差别,但 w/e)).所以当你写时,每个mat-option的值是"jobType.value" 我想这不是你想要的.所以需要把代码改成

In angular, there is a difference between specifying an attribute as value and [value]. When an attribute is enclosed in brackets [], it is interpreted as javascript / angular template script (same as {{}}, I think). When it is not enclosed in brackets, it is interpreted as a string (i.e. value="jobType.value" === [value]="'jobType.value'" and [value]="jobType.value" === value="{{jobType.value}}" (actually I think there are subtle differences between [value]="jobType.value" and value="{{jobType.value}}", but w/e)). So when you write <mat-option *ngFor="let jobType of jobTypes" value="jobType.value">, the value of every mat-option is "jobType.value" which, I imagine, isn't want you want. So you need to change the code to <mat-option *ngFor="let jobType of jobTypes" [value]="jobType.value">

例如

<mat-form-field [formGroup]='editJobInfoForm' fxFlex>
  <mat-label>Job Type</mat-label>
  <mat-select formControlName="JobType" placeholder="Job Type">
    <mat-option *ngFor="let jobType of jobTypes" [value]="jobType.value">
      {{ jobType.DisplayDesc }}
    </mat-option>
  </mat-select>
</mat-form-field>

与您的问题有些无关,为什么同时具有 createForm()initializeForm() 方法?为什么不简单

Somewhat unrelated to your problem, why have both createForm() and initializeForm() methods? Why not simply

constructor(private fb: FormBuilder) {

    ...

        // Construct forms
        this.createForm();    
}

createForm() {
    this.editJobInfoForm = this.fb.group({
        ...
        JobType: this.jobTypes[this.currentJobData.JobType].DisplayDesc,
        ...
    });
}

这篇关于Angular 5 + Angular Material Select + Reactive Forms == 没有显示初始选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆