What I want to achieve here is I would like to mark input field (child node of md-datepicker) should be marked readonly so that user can't enter date value manually (typing) and forced to select date from md-datepicker.
I tried implementing this by decorating md-datepicker directive, but no luck.
is there any other easy and correct way to mark input field as readonly and force user to select date only from calender ?
I'm using Angularjs.
===========================================================================
What I tried is decorating md-datepicker directive and achieve behavior that I want like this
(function () {
'use strict';
angular.module('APPLICATION_NAME').config(['$provide', function($provide) {
$provide.decorator('mdDatepickerDirective', [
'$delegate',
/**
* @function mdDatepickerDirective
* @description decorates mdDatepickerDirective to extend functionality:
* - Mark input field as read-only so which will prevent user from typing date manually
* and should select from date-picker calender only.
* @param {angular.Directive} $delegate
* @returns {angular.Directive} $delegate
*/
function mdDatePickerDecorator($delegate) {
var directive = $delegate[0];
var compile = directive.compile;
directive.compile = function (tElement) {
var link = compile.apply(this, arguments);
tElement.find("input").prop("readOnly", "true");
};
return $delegate;
}
]);
}])})();
But I'm getting some errors like :
- TypeError: Cannot read property '$setTouched' of null
- TypeError: Cannot read property '$setViewValue' of null
What's wrong with element after directive decorator runs ? Pls help.
On focus of input box explicitly calling the datepicker which will open calendar so that user cannot edit the date.
Demo link Example