Javascript 将持有时间戳的ng模型绑定到datetime本地输入
我正在尝试渲染一个Javascript 将持有时间戳的ng模型绑定到datetime本地输入,javascript,angularjs,datetime,timestamp,angular-ngmodel,Javascript,Angularjs,Datetime,Timestamp,Angular Ngmodel,我正在尝试渲染一个它必须是日期对象: $scope.object = { name: 'Demo', value: new Date(1433109600000) } 或创建一个指令: app.directive('bindTimestamp', function () { return { restrict: 'A', require: 'ngModel', link: function (scope, element, attrs, ngModel)
它必须是日期对象:
$scope.object = {
name: 'Demo',
value: new Date(1433109600000)
}
或创建一个指令:
app.directive('bindTimestamp', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
ngModel.$formatters.push(function (value) {
return new Date(value);
});
}
};
});
感谢他,完成@karaxuna的回答,以下是完整的指令及其测试:
directivesApp.directive('bindTimestamp', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
var minDate = attrs.min ? new Date(attrs.min) : null;
var maxDate = attrs.max ? new Date(attrs.max) : null;
ngModel.$formatters.push(function (value) {
var dt = value;
if (!angular.isDate(dt) && angular.isNumber(dt)) {
// It's a timestamp --> needs to be converted to a <Date>
dt = new Date(value);
}
// truncate 'dt' to second if milliseconds are not to be displayed in input[datetime-local]
// dt.setMilliSeconds(0);
// truncate 'dt' to minute if seconds are not to be displayed in input[datetime-local]
// dt.setSeconds(0);
return dt;
});
// Default validators need to be overriden, otherwise they will always trigger "false" as they expect a <Date> object and not a number.
var MinMaxValidator = function (modelValue) {
var selectedDate = angular.timestampToDate(modelValue, scope.truncateSeconds);
this.validate = function (validateThreshold) {
if (angular.isDate(selectedDate)) {
return validateThreshold(selectedDate);
}
return true;
};
};
if (ngModel.$validators.min && minDate !== null) {
ngModel.$validators.min = function (modelValue) {
return new MinMaxValidator(modelValue).validate(function (selectedDate) {
return minDate <= selectedDate;
});
};
}
if (ngModel.$validators.max && maxDate !== null) {
ngModel.$validators.max = function (modelValue) {
return new MinMaxValidator(modelValue).validate(function (selectedDate) {
return maxDate >= selectedDate;
});
};
}
}
};
});
directivesApp.directive('bindTimestamp',function(){
返回{
限制:“A”,
要求:'ngModel',
链接:功能(范围、元素、属性、模型){
var minDate=attrs.min?新日期(attrs.min):空;
var maxDate=attrs.max?新日期(attrs.max):空;
ngModel.$formatters.push(函数(值){
var dt=值;
如果(!angular.isDate(dt)和&angular.isNumber(dt)){
//它是一个时间戳-->需要转换为
dt=新日期(值);
}
//如果输入[datetime local]中不显示毫秒,则将“dt”截断为秒
//dt.setms(0);
//如果输入[datetime local]中不显示秒,则将“dt”截断为分钟
//dt.设置秒(0);
返回dt;
});
//需要重写默认验证器,否则它们将始终触发“false”,因为它们期望的是对象而不是数字。
var MinMaxValidator=函数(modelValue){
var selectedDate=angular.timestampToDate(modelValue,scope.truncateSeconds);
this.validate=函数(validateThreshold){
如果(角度isDate(选定日期)){
返回validateThreshold(selectedDate);
}
返回true;
};
};
if(ngModel.$validators.min&&minDate!==null){
ngModel.$validators.min=函数(modelValue){
返回新的MinMaxValidator(modelValue).validate(函数(selectedDate){
return minDate=selectedDate;
});
};
}
}
};
});
茉莉花试验:
描述('bindTimestamp:timestamps',函数(){
//2016-02-19 09:56:51.396ms
var dt=1455872211396;
函数buildInputDateElement(inputType){
变量元素=角度元素(“”);
$compile(元素)($scope);
$scope.$digest();
返回元素;
}
它('bindTimestamp:input[date]',函数(){
var FormateDate='2016-02-19';
/**时间戳*/
$scope.myDate=dt;
var元素=buildInputDateElement(“日期”);
expect(element.val()).toEqual(formattedate);
//**已经是*/
$scope.myDate=新日期(dt);
元素=buildInputDateElement(“日期”);
expect(element.val()).toEqual(formattedate);
});
它('bindTimestamp:input[datetime local]',函数(){
var FormateDate='2016-02-19T09:56:51.396';
/**时间戳*/
$scope.myDate=dt;
var元素=buildInputDateElement(“本地日期时间”);
expect(element.val()).toEqual(formattedate);
/**已经是*/
$scope.myDate=新日期(dt);
element=buildInputDateElement(“本地日期时间”);
expect(element.val()).toEqual(formattedate);
});
});
这一点很清楚,但如上所述:对象来自Web服务,因此我无法修改值……为什么不能?例如:return$http.get(“…”).then(函数(结果){result.value=newdate(result.value);returnresult})代码>-这将被修改,另一种方法是为此编写指令。我也会写的,等一下)我想指令是正确的方式。我真的不知道我在返回对象的哪个级别上得到了时间戳,并且遍历整个对象是昂贵的
app.directive('bindTimestamp', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
ngModel.$formatters.push(function (value) {
return new Date(value);
});
}
};
});
directivesApp.directive('bindTimestamp', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
var minDate = attrs.min ? new Date(attrs.min) : null;
var maxDate = attrs.max ? new Date(attrs.max) : null;
ngModel.$formatters.push(function (value) {
var dt = value;
if (!angular.isDate(dt) && angular.isNumber(dt)) {
// It's a timestamp --> needs to be converted to a <Date>
dt = new Date(value);
}
// truncate 'dt' to second if milliseconds are not to be displayed in input[datetime-local]
// dt.setMilliSeconds(0);
// truncate 'dt' to minute if seconds are not to be displayed in input[datetime-local]
// dt.setSeconds(0);
return dt;
});
// Default validators need to be overriden, otherwise they will always trigger "false" as they expect a <Date> object and not a number.
var MinMaxValidator = function (modelValue) {
var selectedDate = angular.timestampToDate(modelValue, scope.truncateSeconds);
this.validate = function (validateThreshold) {
if (angular.isDate(selectedDate)) {
return validateThreshold(selectedDate);
}
return true;
};
};
if (ngModel.$validators.min && minDate !== null) {
ngModel.$validators.min = function (modelValue) {
return new MinMaxValidator(modelValue).validate(function (selectedDate) {
return minDate <= selectedDate;
});
};
}
if (ngModel.$validators.max && maxDate !== null) {
ngModel.$validators.max = function (modelValue) {
return new MinMaxValidator(modelValue).validate(function (selectedDate) {
return maxDate >= selectedDate;
});
};
}
}
};
});
// 2016-02-19 09:56:51.396ms
var dt = 1455872211396;
function buildInputDateElement(inputType) {
var element = angular.element('<input type="' + inputType + '" ng-model="myDate" bind-timestamp>');
$compile(element)($scope);
$scope.$digest();
return element;
}
it('bindTimestamp : input[date]', function () {
var formatedDate = '2016-02-19';
/** A timestamp */
$scope.myDate = dt;
var element = buildInputDateElement("date");
expect(element.val()).toEqual(formatedDate);
//** Already a <Date> */
$scope.myDate = new Date(dt);
element = buildInputDateElement("date");
expect(element.val()).toEqual(formatedDate);
});
it('bindTimestamp : input[datetime-local]', function () {
var formatedDate = '2016-02-19T09:56:51.396';
/** A timestamp */
$scope.myDate = dt;
var element = buildInputDateElement("datetime-local");
expect(element.val()).toEqual(formatedDate);
/** Already a <Date> */
$scope.myDate = new Date(dt);
element = buildInputDateElement("datetime-local");
expect(element.val()).toEqual(formatedDate);
});
});