【问题标题】:Select control binding doesn't working in Angular 8选择控件绑定在 Angular 8 中不起作用
【发布时间】:2020-06-09 19:24:53
【问题描述】:

在检查了几个回复之后,我在这里提出这个问题,因为我在绑定“mat-select”输入时遇到了一些问题。

这是component.ts文件中的代码

  selectedIndustry : any;  
  constructor(private industrysectorService: IndustrySectorService, private _formBuilder: FormBuilder) {}

  ngOnInit() {

    this.industrysectorService.get().subscribe((response: IResponse<IIndustrySector[]>) => {
      if (response.isSuccessful) {
        this.industries = response.result.list;        
      } else {
        this.industries = [];        
      }
    });

    this.selectedIndustry = this.industries[0];

这在 componente.html 文件中。

<mat-label>Industry Sector</mat-label>
      <mat-select required type="number" [(ngModel)]="selectedIndustry" [ngModelOptions]="{standalone: true}">
        <option *ngFor="let industry of industries" [ngValue]="industry.id">{{industry.industrySectorName}}</option>   
    </mat-select>

控制台中的错误信息是:

EngagementDetailsComponent.html:43 ERROR Error: Cannot find control with unspecified name attribute
at _throwError (forms.js:3357)
at setUpControl (forms.js:3181)
at FormControlDirective.ngOnChanges (forms.js:7102)
at checkAndUpdateDirectiveInline (core.js:31906)
at checkAndUpdateNodeInline (core.js:44367)
at checkAndUpdateNode (core.js:44306)
at debugCheckAndUpdateNode (core.js:45328)
at debugCheckDirectivesFn (core.js:45271)
at Object.eval [as updateDirectives] (EngagementDetailsComponent.html:48)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:45259)

并且数据绑定不起作用。 变量:“selectedIndustry”和“industries”已从数据库中正确填充。 我检查了this 示例,但我无法确定问题出在哪里。 谢谢!

【问题讨论】:

    标签: angular select binding angular-material angular8


    【解决方案1】:

    您正在混合mat-select 和普通选择。请改用 mat-option[value] 属性绑定。 More Info

    <mat-select>
        <mat-option *ngFor="let food of foods" [value]="food">
          {{food}}
        </mat-option>
      </mat-select>
    

    【讨论】:

    • 比你约瑟夫!除了你告诉我的,正确的属性是 [value]="industry.id" 。我可以修复它与你的回复!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 2018-08-14
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多