2015-10-07 136 views
0

我有一个带有选择框的表单来选择企业的工作时间。我想选择上午9:00作为第一个框的默认值,下午5:00作为第二个框的默认值。如何使用ng选项在选择框中选择默认时间值

这里是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'格式在选择框中显示时间,因此默认值显示不正确的原因是什么?

回答

1

只需将模型start_time设置正确,例如在您的控制器中!

<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 

使用最新版本的角度和track by hour解决了这个问题:http://jsfiddle.net/j8xu1h2h/

+0

嘿,我居然在的jsfiddle增加了$ scope.start_time,现在看到了。忘记在 – achabacha322

+0

之前加入它找不到:( –

+0

更新了问题中的链接:http://jsfiddle.net/waleedasif322/9178azcm/4/ – achabacha322