I am working on angularjs application. User can select date from the From Date picker and To Date picker fields. I want to disable the date selection in the To Date picker based on the date user selected in From Date picker field. eg) If user has selected 02/23/2017 in From Date picker field, all the dates before 02/23/2017 should be disabled in To Date picker.
I tried assigning model1 to the min-date attribute of To Date as shown below but that was not working. And even tried to display To-Date field date picker calendar pop up as soon as the user has selected the date in the From-Date field but ended up with javascript errors. Any suggestions would be helpful.
<input type="text" uib-datepicker-popup="{{dateformat}}" min-date={{model1}} ng-model="model2" is-open="showdp" />
Below is the code:
<div style="float:left" ng-controller="fromDateCtrl">
From Date:
<input type="text" uib-datepicker-popup="{{dateformat}}" min-date="mindate" ng-model="model1" is-open="showdp" />
<span>
<button type="button" class="btn btn-default" ng-click="showcalendar($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
<div style="float:left" ng-controller="toDateCtrl">
To Date:
<input type="text" uib-datepicker-popup="{{dateformat}}" min-date="mindate" ng-model="model2" is-open="showdp" />
<span>
<button type="button" class="btn btn-default" ng-click="showcalendar($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
js code:
angular.module('plunker', ['ngAnimate', 'ui.bootstrap']);
angular.module('plunker').controller('fromDateCtrl', function ($scope) {
$scope.today = function () {
$scope.model1 = new Date();
};
$scope.mindate = new Date();
$scope.dateformat="MM/dd/yyyy";
$scope.today();
$scope.showcalendar = function ($event) {
$scope.showdp = true;
};
$scope.showdp = false;
});
angular.module('plunker').controller('toDateCtrl', function ($scope) {
$scope.today = function () {
$scope.model2 = new Date();
};
$scope.mindate = new Date();
$scope.dateformat="MM/dd/yyyy";
$scope.today();
$scope.showcalendar = function ($event) {
$scope.showdp = true;
};
$scope.showdp = false;
});