Han*_*tsy 5 javascript twitter-bootstrap angularjs
我花了一些时间研究 angularjs 的现有日期时间指令。
ngularUI 和 AngularStrap 都没有提供我需要的日期时间选择器。当然,我知道一起使用 datepicker 和 timepicker 来存档目的。
我已经从 Internet 和 stackoverflow 搜索了相关主题。发现了一些有趣且有用的信息。
http://dalelotts.github.io/angular-bootstrap-datetimepicker/,有一个 datetimepicker,但我不喜欢这个指令的用法。
将 datetimepicker 连接到 angularjs,这个主题非常有帮助,我尝试按照步骤包装我的 datetimepicker 指令。
我的工作基于https://github.com/Eonasdan/bootstrap-datetimepicker,一个基于 bootstrap 3 的 datetimepicker,UI 非常好。
app.directive('datetimepicker', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
console.log('call datetimepicker link...');
var picker = element.datetimepicker({
dateFormat: 'dd/MM/yyyy hh:mm:ss'
});
//ngModelCtrl.$setViewValue(picker.getDate());
//model->view
ngModelCtrl.$render(function() {
console.log('ngModelCtrl.$viewValue@'+ngModelCtrl.$viewValue);
picker.setDate(ngModelCtrl.$viewValue || '');
});
//view->model
picker.on('dp.change', function(e) {
console.log('dp.change'+e.date);
scope.$apply(function(){
ngModelCtrl.$setViewValue(e.date);
});
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
并在我看来使用它。
<div class="col-md-6">
<div class="input-group date" id="endTime" data-datetimepicker ng-model="stat.endTime" data-date-format="MM/DD/YYYY hh:mm A/PM" >
<input class="form-control" type="text" placeholder="End"/>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我发现了一些问题。
提前致谢。
I had the same issue. Here's what I ended up doing that worked well for me:
'use strict';
angular.module('frontStreetApp.directives')
.directive('psDatetimePicker', function (moment) {
var format = 'MM/DD/YYYY hh:mm A';
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attributes, ctrl) {
element.datetimepicker({
format: format
});
var picker = element.data("DateTimePicker");
ctrl.$formatters.push(function (value) {
var date = moment(value);
if (date.isValid()) {
return date.format(format);
}
return '';
});
element.on('change', function (event) {
scope.$apply(function() {
var date = picker.getDate();
ctrl.$setViewValue(date.valueOf());
});
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
Here's the HTML:
<!-- The dueDate field is a UNIX offset of the date -->
<input type="text"
ng-model="dueDate"
ps-datetime-picker
class="form-control">
Run Code Online (Sandbox Code Playgroud)
You can check out the gists and a bit more information in my blog post.
小智 0
可以帮助你解决第一点;将$watch添加到链接函数内的元素并设置
值=“{{$scope.variableWithTheInitialDate}}”在文本字段中;这样加载时,日期时间选择器就已经设置好了。| 归档时间: |
|
| 查看次数: |
18024 次 |
| 最近记录: |