【发布时间】:2018-07-25 14:51:51
【问题描述】:
我正在尝试以编程方式设置 2 个字段 <input matInput> abnd <mat-select> 的值。对于文本输入,一切都按预期工作,但是对于视图上的<mat-select>,这个字段就像它在null 上的值一样。但是,如果我调用console.log(productForm.controls['category'].value,它会打印出我以编程方式设置的正确值。我错过了什么吗?
代码如下:
表单配置:
productForm = new FormGroup({
name: new FormControl('', [
Validators.required
]),
category: new FormControl('', [
Validators.required
]),
});
设定值:
ngOnInit() {
this.productForm.controls['name'].setValue(this.product.name);
this.productForm.controls['category'].setValue(this.product.category);
}
}
html:
<mat-form-field>
<mat-select [formControlName]="'category'"
[errorStateMatcher]="errorStateMatcher">
<mat-option *ngFor="let category of categories" [value]="category">
{{category.name}}
</mat-option>
</mat-select>
</mat-form-field>
【问题讨论】:
标签: angular angular2-forms angular-material2 angular5 angular-reactive-forms