Angular: 如何设置 'value' 与选项中显示的值不同?
Angular: how to set 'value' different than displayed value in options?
在我的 angular SPA 中,我使用 <select><option ng-repeat...></select>
构造,这样
<div ng-init="hours = [ '9:00', '10:00' ]">
<select name="eta" ng-model="reservation.eta">
<option ng-repeat="hour in hours track by $index" ng-value="hour">{{ 'We arrive at' | translate }} {{hour}}</option>
</select>
</div>
问题是在每个 option
的 value
中我得到 "We arrive at HOUR" 字符串(与显示值相同,这是正确的),而不是单独的小时,如我所愿...
我错过了什么?
N.B.: 请不要建议使用 ng-options
因为我因为设置选中和禁用的第一个选项(作为占位符)的一些问题而被迫放弃它。 .. :-(
这里不需要使用ng-value。就用价值吧。
<div ng-init="hours = [ '9:00', '10:00' ]">
<select name="eta" ng-model="reservation.eta">
<option ng-repeat="hour in hours track by $index" value="{{hour}}">{{ 'We arrive at' | translate }} {{hour}}</option>
</select>
</div>
在我的 angular SPA 中,我使用 <select><option ng-repeat...></select>
构造,这样
<div ng-init="hours = [ '9:00', '10:00' ]">
<select name="eta" ng-model="reservation.eta">
<option ng-repeat="hour in hours track by $index" ng-value="hour">{{ 'We arrive at' | translate }} {{hour}}</option>
</select>
</div>
问题是在每个 option
的 value
中我得到 "We arrive at HOUR" 字符串(与显示值相同,这是正确的),而不是单独的小时,如我所愿...
我错过了什么?
N.B.: 请不要建议使用 ng-options
因为我因为设置选中和禁用的第一个选项(作为占位符)的一些问题而被迫放弃它。 .. :-(
这里不需要使用ng-value。就用价值吧。
<div ng-init="hours = [ '9:00', '10:00' ]">
<select name="eta" ng-model="reservation.eta">
<option ng-repeat="hour in hours track by $index" value="{{hour}}">{{ 'We arrive at' | translate }} {{hour}}</option>
</select>
</div>