带有选择列表的ngModel

时间:2018-12-11 12:36:21

标签: html angularjs typescript drop-down-menu angular-ngmodel

我无法从选择元素中输出值。

<select 
([ngModel])="office_hour_start" 
name="office_hour_start">
  <option 
    *ngFor="let time of times"
    value="{{time.i}}">
    {{ time.i }}
  </option>
</select>

此设置对我有用:

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

2 个答案:

答案 0 :(得分:1)

在对您有用的设置上,另一建议是:

// When using an ngModel, an event emitter () should be inside the input [] always 
// Though there is an instance where you can use it separately [ngModel] for Input (ngModelChange) for event emitter
<select [(ngModel)]="employee_id"           
        name="employee_id" 
        class="form-control">

    <option *ngFor="let employee of employees"
            value="{{employee.id}}">       // use [value]="employee.id" if you're assigning a dynamic value
        {{ employee.name }}
    </option>

</select>
  

对于 office_hour_start 而言,其是ngModel。你用过   ([[ngModel]) =“ office_hour_start”代替 [(ngModel)] =“ office_hour_start”将括号放在方括号[]中是解决问题的方法。 / p>

已经创建了一个Stackblitz Demo供您参考

答案 1 :(得分:0)

    <select ng-options="time as time.i for time in office_hour_start track by time.id" ng-model="selected">
<option value="{{time.i}}">{{time.i}}</option>
</select>

希望这会有所帮助,请使用ng-options而不是ng-for