I've defined a directive like so:
angular.module('MyModule', [])
.directive('datePicker', function($filter) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
ctrl.$formatters.unshift(function(modelValue) {
console.log('formatting',modelValue,scope,elem,attrs,ctrl);
return $filter('date')(modelValue, 'MM/dd/yyyy');
});
ctrl.$parsers.unshift(function(viewValue) {
console.log('parsing',viewValue);
var date = new Date(viewValue);
return isNaN(date) ? '' : date;
});
}
}
});
Which I apply to an element like so:
<input type="text" date-picker="MM/dd/yyyy" ng-model="clientForm.birthDate" />
My directive gets triggered whenever I add the date-picker
attribute to an element, but I want to know how to access the attribute's value (MM/dd/yyyy
) inside my directive JS so that I can remove that constant beside $filter
. I'm not sure if any of the variables I have access to provide this.