在选项中使用ngModel选择

时间:2018-12-11 10:48:00

标签: html angular typescript api angular-ngmodel

我想将属性从api调用传递给Submit函数,但不确定为什么它不起作用。我认为这与选择或重新格式化输入有关。

遇到错误Error: No value accessor for form control with name: 'employee_id'

form.component.html

<select>
 <option 
   *ngFor="let employee of employees" 
   name="employee_id" 
   [(ngModel)]="employee_id">
   {{ employee.name }}
 </option>
</select>

<button 
  (click)="submitMeetingRequirements()" 
  class="btn btn-success">
  Suggest Meeting
</button>

这是来自form.component.ts

的提交功能
submitMeetingRequirements() {
  this._meetingService.submitMeetingRequirements(
    this.employee_id)
    .subscribe(res => this.suggestions = res["suggestions"])
}

2 个答案:

答案 0 :(得分:1)

已修复

<select [(ngModel)]="employee_id" name="employee_id">
  <option 
    *ngFor="let employee of employees"
    value="{{employee.id}}">
    {{ employee.name }}
  </option>
</select>

答案 1 :(得分:0)

select标签上使用 [(ngModel)] ,而不要在option标签上使用。像这样:

<select [(ngModel)]="employee_id">
 <option 
   *ngFor="let employee of employees" 
   name="employee_id" >
   {{ employee.name }}
 </option>
</select>

<button 
  (click)="submitMeetingRequirements()" 
  class="btn btn-success">
  Suggest Meeting
</button>

这是您推荐的Working Sample StackBlitz