I am attempting to encapsulate an <input>
within a directive in order to manage date validation, conversion from string to Date
object, and keep the Date
version in the original scope. The functionality seems to be working as intended. However, the ng-pattern
attribute on the <input>
element is not functioning properly. It does not invalidate the <input>
, regardless of the input.
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;
}
});
}
};
Check out the JSFiddle for this code snippet here: https://jsfiddle.net/e5qu5rgy/1/
I would greatly appreciate any assistance with this issue!