我有一个带有选择框的表单,用于选择企业的工作时间。我想选择上午 9:00 作为第一个框的默认值,选择下午 5:00 作为第二个框的默认值。
这是JSfiddle
比如我有
<select ng-options="hour as (hour | date: 'shortTime') for hour in hours"
ng-model="start_time" ng-change="update(start_time, $index, 1)">
如果在我的 app.js 中,我将 $scope.start_time
设置为等于初始化为当前日期和时间(上午 9:00)的 Date 对象,则默认值将被选为中的最后一个值而是选择框。由于我使用日期过滤器以“shortTime”格式在选择框中显示时间,这是否是默认值未正确显示的原因?
最佳答案
只需正确设置模型start_time
,例如在 Controller 中!
<select ng-options="hour as (hour | date: 'shortTime') for hour in hours track by hour"
ng-model="start_time" ng-change="update(start_time, $index, 1)">
$scope.start_time = new Date(); // set default date in controller
使用最新的 Angular 版本和按小时跟踪
解决了问题:http://jsfiddle.net/j8xu1h2h/
关于javascript - 如何使用 ng-options 在选择框中选择默认时间值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32999234/