简体   繁体   English

如何在带有ng-options的选择框中选择默认时间值

[英]How to choose default time value in a select box with ng-options

I have a form with select boxes to choose a business' working hours. 我有一个带有选择框的表格,用于选择公司的工作时间。 I want to choose 9:00 am as the default value for the first box, and 5:00 pm as the default value for the second box. 我想选择上午9:00作为第一个框的默认值,并选择5:00 pm作为第二个框的默认值。

Here is the JSfiddle 这是JSfiddle

For example, I have 例如,我有

<select ng-options="hour as (hour | date: 'shortTime') for hour in hours"
  ng-model="start_time" ng-change="update(start_time, $index, 1)">

If in my app.js I set $scope.start_time equal to a Date object initialized to the current date and time as 9:00 am, the default value is chosen as the last value in the select box instead. 如果在我的app.js中,我将$scope.start_time设置$scope.start_time一个Date对象,该对象初始化为当前日期和时间为上午9:00,则将默认值选择为选择框中的最后一个值。 Since I am using a date filter to display the time in the select box in the 'shortTime' format, is that the reason why the default value is not being shown correctly? 由于我使用日期过滤器以“ shortTime”格式在选择框中显示时间,是因为默认值显示不正确吗?

Just set the model start_time right, for example in your controller! 只需将模型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

Using the latest angular version and track by hour solves the problem: http://jsfiddle.net/j8xu1h2h/ 使用最新的角度版本并按track by hour可解决此问题: http : //jsfiddle.net/j8xu1h2h/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM