Mik*_*son 8 c# datetime date angularjs
我有一个Angular集合绑定到一个从MVC JsonResult填充的转发器.
$scope.loadData = function () {
$http.get('/Product/GetDiscountCodes')
.then(function (result) {
console.log(result);
$scope.discountCodes = result.data.DiscountCodes;
$scope.selected = {};
});
};
Run Code Online (Sandbox Code Playgroud)
转发器中的一个字段填充了日期.从服务器返回的日期是C#DateTime,如下所示:
2015年1月5日上午12:02:00
但是当它被Angular绑定时,显示如下:
/日期(1420434120000)/
如何在mm/dd/yyyy格式的文本字段中正确显示日期?
我尝试了ui-utils日期格式但是没有用.
我也尝试创建自己的指令,正确格式化日期,但是当发生保存事件时,发送到服务器端方法的日期全部被抬起.
function inputDate() {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelController) {
ngModelController.$parsers.push(function (data) {
if (data === null) { return null; }
var d = moment(data).format('YYYY-MM-DD');
return d;
});
ngModelController.$formatters.push(function (data) {
if (data === null) { return null; }
return moment(data).format('MM/DD/YYYY');
});
}
}
}
angular.module('discountApp')
.directive('inputDate', inputDate);
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
var app = angular.module('app',[]);
app.filter('ctime', function(){
return function(jsonDate){
var date = new Date(parseInt(jsonDate.substr(6)));
return date;
};
});
app.controller('fCtrl', function($scope){
$scope.date = '/Date(1420875802707)/';
});
Run Code Online (Sandbox Code Playgroud)