1
我試圖在指令中包裝<input>
,以便我可以處理日期驗證並將其從字符串轉換爲實際的Date
對象並將Date
版本保留在原始範圍內。這種互動按預期工作。但<input>
元素上的ng-pattern
行事不正確。無論輸入什麼,它都不會使<input>
無效。ng-pattern在指令內不起作用
HTML
<pl-date date="date"></pl-date>
JS
.directive("plDate", function (dateFilter) {
return {
restrict: 'E',
replace: true,
template: '<input id="birthDateDir" name="birthDate" type="text" ng-pattern="{{getDatePattern()}}" ng-model="dateInput">',
scope: {
date: '='
},
link: function (scope) {
scope.dateInput = dateFilter(scope.date, 'MM/dd/yyyy');
scope.$watch('date', function (newVal) {
if (newVal !== scope.tmp) {
if (!newVal) {
scope.dateInput = null;
} else {
scope.dateInput = dateFilter(scope.date, 'MM/dd/yyyy');
}
}
});
scope.getDatePattern = function() {
var exp = '/';
// Removed for brevity
exp += '/';
return exp;
};
scope.$watch('dateInput', function (newVal) {
if (newVal !== null) {
scope.date = new Date(newVal);
scope.tmp = scope.date;
}
});
}
};
的jsfiddle這裏:https://jsfiddle.net/e5qu5rgy/1/
任何幫助就大家大大感謝!