我在从select元素输出值时遇到问题。
<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>
关于适用于您的设置,一个额外的建议是:
// 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_hous_start"将括号放在括号[]内是解决问题的方法。
为您的参考创建了Stacklitz演示
<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选项而不是的ng