I am beginner in Angular JS and I am currently trying to use datepicker from Angular UI library.
The problem that i want to solve is that when I open the first but don't pick a date and then click on the second datepicker, the second just go over the first one, then I want that when I click on the second datepicker it automatically close the first one, I have tried many solutions but any of those work so if you have any idea, let me know !
Here is my HTML markup:
<div class="col-md-2">
<p class="input-group date-picker" data-ng-controller="DatePickerController">
<input type="text" name="check-in" placeholder="Check in" class="form-control" datepicker-popup="{{ format }}" data-ng-model="dt" is-open="opened1" min-date="minDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default icon-field" ng-click="open1($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
<div class="col-md-2">
<p class="input-group date-picker" data-ng-controller="DatePickerController">
<input type="text" name="check-out" placeholder="Check out" class="form-control" datepicker-popup="{{ format }}" data-ng-model="dt" is-open="opened2" min-date="minDate" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default icon-field" ng-click="open2($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
And here is the Javascript: controllers.DatePickerController = function($scope) {
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
};
$scope.toggleMin = function() {
$scope.minDate = $scope.minDate ? null : new Date();
};
$scope.toggleMin();
$scope.open1 = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened2 = false;
$scope.opened1 = true;
};
$scope.open2 = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened1 = false;
$scope.opened2 = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
$scope.formats = ['dd/MM/yyyy', 'dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
};
Thanks in advance !
EDIT: The problem went from the data-ng-controller declaration, because a new controller was declarated for each one, the scope was not "shared", beginner mistake ! Thank you for your help !