Html 使用 angular.js 输入类型时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24984681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input type time with angular.js
提问by Alx
According to the Docs input[time]: https://docs.angularjs.org/api/ng/input/input%5Btime%5D
根据文档输入[时间]:https: //docs.angularjs.org/api/ng/input/input%5Btime%5D
it should be enough to use the input type time and bind it to a date oject, however it doesn't work as I'd expect it.
使用输入类型时间并将其绑定到日期对象应该就足够了,但是它不能像我期望的那样工作。
<input ng-model="time" type="time" placeholder="HH:mm" min="08:00" max="17:00" required >
and
和
$scope.time = new Date();
as a Result I'd like to see just the HH:mm within the input field.
结果我只想在输入字段中看到 HH:mm。
Here's a jsfiddle to play around:
这是一个可以玩的 jsfiddle:
回答by Matsemann
回答by mrtvc
You could achieve that, follow my code, http://plnkr.co/edit/60aiH0eJ8ee0FlEsQE2m?p=info
你可以做到这一点,按照我的代码, http://plnkr.co/edit/60aiH0eJ8ee0FlEsQE2m?p=info
Basically i use momentjs, and set seconds and milliseconds to zero, that way browser will not render that.
基本上我使用momentjs,并将秒和毫秒设置为零,这样浏览器就不会呈现。
moment().second(0).milliseconds(0).toDate();
回答by Abdallah Okasha
Here's an example of how to use input="time" with angularJS and bind it using ng-model directive
这是一个如何在 angularJS 中使用 input="time" 并使用 ng-model 指令绑定它的示例
HTML: <input type="time" ng-model="myTime"/>
HTML: <input type="time" ng-model="myTime"/>
in the controller, we assign time came from database to date a new object
在控制器中,我们分配来自数据库的时间到日期一个新对象
$scope.myTime = new Date(response.data.start_time);
this will parse the full date as a time HH:MM:SS Successfully
这会将完整日期解析为时间 HH:MM:SS 成功
回答by Aniket Suryavanshi
Change $scope.time = new Date();
in your code to:
$scope.time = new Date();
将您的代码更改为:
var d = new Date();
$scope.time = d.getHours() + ':' + d.getMinutes();
Working code: http://jsfiddle.net/bitsmith/asjv8ycq/1/