Выбор контрольной привязки не работает в Angular 8 - PullRequest
1 голос
/ 26 февраля 2020

После проверки нескольких ответов, я здесь, чтобы задать этот вопрос, потому что у меня есть некоторые проблемы с привязкой ввода "mat-select".

это код в component.ts file

  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];

и это в компоненте. html file.

<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)

и данные Bingind не работает. переменные: «selectedIndustry» и «industries» были правильно заполнены из БД. Я проверил этот пример, но я не могу понять, в чем проблема. Спасибо!

enter image description here

1 Ответ

1 голос
/ 26 февраля 2020

Вы смешиваете mat-select и выбираете нормально. Пожалуйста, используйте mat-option с привязкой атрибута [value]. Подробнее

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