【发布时间】:2016-10-26 07:05:13
【问题描述】:
我编写了这个自定义组件:
<datetime value="vm.woComplete.CompleteDate" max="2016-10-25"></datetime>
max 属性在 html5 验证日期输入中不起作用。
为什么?
// DateTimeComponent is used for date/time input with 2-way binding and UTC correction
namespace AppDomain {
"use strict";
class DateTimeController {
value: Date;
displayValue: Date;
max: string;
static $inject = ["$scope"];
// West from UTC - TimezoneOffset is positive , East from UTC - TimezoneOffset is negative
constructor($scope: ng.IScope) {
$scope.$watch(
"$ctrl.displayValue",
(newValue: Date, oldValue: Date) => {
this.value = newValue;
var offset = this.displayValue.getTimezoneOffset() / 60; // getTimezoneOffset returns minutes, we need hours
this.value.setHours(this.value.getHours() + offset); // LOCAL to UTC = UTC + Offset
}
);
}
$onInit() {
this.displayValue = this.value;
var offset = this.displayValue.getTimezoneOffset() / 60; // getTimezoneOffset returns minutes, we need hours
this.displayValue.setHours(this.displayValue.getHours() - offset); // UTC to LOCAL = UTC - Offset
}
}
const DateTimeComponent: ng.IComponentOptions = {
//template: "<p><input type='date' class='form-control' ng-model='$ctrl.displayValue'></p><p><input type='time' class='form-control' ng-model='$ctrl.displayValue'></p>",
template: "<p><input type='datetime-local' class='form-control' ng-model='$ctrl.displayValue' max='{{$ctrl.max}}'></p>",
bindings: {
value: "=",
max: "@"
},
controller: DateTimeController
};
angular.module("app").component("datetime", DateTimeComponent);
}
【问题讨论】:
标签: angularjs html typescript