angular - 使用具有反应形式的 Angular Material 垫选择时出错

标签 angular angular-material angular7 angular-material-7

我正在尝试将 Angular Material mat-select 与 react 形式一起使用,并收到错误消息“没有名称为“productUnitofMeasure”的表单控件的值访问器”。

其他 FormControls 在这里工作正常,我已经在应用程序模块中包含了所有必需的模块。

应用程序模块:

import {MatFormFieldModule, MatOptionModule, MatSelectModule, MatInputModule} from '@angular/material';

imports:[
MatFormFieldModule,
MatOptionModule,
MatSelectModule,
MatInputModule,
ReactiveFormsModule]

模板:

<mat-form-field>
<mat-select placeholder="Unit Type">
    <mat-option *ngFor="let unitType of unitList" matInput formControlName="productUnitofMeasure" [value]="unitType.unitId">{{unitType.unitDescription}}</mat-option>
</mat-select>

组件:

this.productForm = new FormGroup({
  productName: new FormControl,
  productDescription: new FormControl,
  productPrice: new FormControl,
  productAvailableQuantity: new FormControl,
  productUnitofMeasure: new FormControl //this is the only control giving me an error.


});

最佳答案

You should use formControlName in mat-select not in mat-option

<mat-form-field>
<mat-select placeholder="Unit Type" formControlName="productUnitofMeasure" >
    <mat-option *ngFor="let unitType of unitList" matInput [value]="unitType.unitId">{{unitType.unitDescription}}</mat-option>
</mat-select>

关于angular - 使用具有反应形式的 Angular Material 垫选择时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53035484/

相关文章:

angularjs - 是否可以在页面顶部放置一个 $mdToast 以实现窄视口(viewport)?

Angular 7 : How to access query string in URL from an iframe?

angular - 如何使用 Angular 组件中的 DecimalPipe?

Angular Material Datepicker 不适合我

javascript - Angular Material md-datepicker 和 md-select 验证

javascript - 如何在 Angular 7 中使用 + 号验证国家代码?

javascript - Angular 让条件元素自动滚动到 View 中的最佳方法

javascript - 是否可以从 rxjs-timeout-error 中获取请求信息?

angular - 如何为 Angular 5 的 PrimeNG TreeTable 创建数据模型?

javascript - 如何将 Web 应用程序中基于旧 dojo 的 UI 重写为新的 Angular 2 框架?