我有从MVC WEB API控制器(2013-09-17 15:18:53Z)获取的JSON格式的日期。我在与angularjs一起使用jquery datepicker。我有需要根据文化显示日期的输入元素,但是为了节省时间,我再次需要使用JSON格式的日期。
Currenlty我面临一个无效的问题。您能否在指令中建议处理它的最佳方法。
ServerSide:WebAPI
public class MyModel
{
public int Id { get; set; }
public DateTime? MyDate { get; set; }
}
[HttpGet]
[Route("")]
public MyModel GetInfo(int id)
{
MyModel model = GetInfo(id);
return model;
}
public static MyModel GetInfo (int id)
{
MyModel model = new model();
model.Id = id;
model.MyDate = DateTime.UtcNow;
}
客户端:
Directive Jquery DatePicker:
angular.module('common').directive('datePicker', function ($locale) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
$(function () {
element.datepicker({
onSelect: function (date) {
scope.$apply(function () {
ngModelCtrl.$setViewValue(date);
});
}
});
});
}
}
});
UI:
<input type="text" name="Mydate" data-ng-model="vm.data.MyDate" date-picker />
已加载服务:
myService.getInfo(id)
.then(
function (load) {
vm.data = load.data;
vm.data.Mydate = $filter('date')(load.data.MyDate, 'shortDate');
});
为什么主要在AngularJS中使用jQuery控件?考虑使用AngularJS's stock datepicker。
注释中建议的3rd-party ngDatepicker多年未更新,演示已死,并且代码可能与现代AngularJS不兼容。
您可以在提供的链接中找到AngularJS官方datepicker的示例和实时演示。