title: Odd behaviour with multiple datepickers using AngularJS and UI Bootstrap
body: <p>I'm writing an app using AngularJS and UI Bootstrap, and I'm trying to implement a modal where the user can choose a start date and an end date for an event. The problem I'm having is that when the user is chosing an end date, the start date is affected as well, leading to incorrect values.</p>
  
  <p>I have made a minimal test case here: <a href="http://147.75.205.135/test/" rel="nofollow">http://147.75.205.135/test/</a></p>
  
  <p><strike>(I tried to reproduce it using Plunkr, but for some reason I didn't get the same behaviour there)</strike></p>
  
  <p>Plunker link: <a href="http://plnkr.co/edit/kika1viHi8csXMuklw8Z" rel="nofollow">http://plnkr.co/edit/kika1viHi8csXMuklw8Z</a></p>
  
  <p>To recreate the issue, click the "Launch" button, open up the datepicker for the start date, and click the right arrow to go to the next month and choose a date. Then for the end date, click the right arrow to go to the next month and pick any date. Then click "Save". An alert box will show up displaying the start and end date, but the start date does not correspond to the one chosen in the datepicker, it is set to the 1st of the month chosen for the end date.</p>
  
  <p>I have nailed it down to the following function in <a href="http://147.75.205.135/test/script/angular/main.js" rel="nofollow">http://147.75.205.135/test/script/angular/main.js</a>:</p>
  
  <pre><code>$scope.adjustEndDate =  function() {
    if ($scope.endDate &lt; $scope.startDate) {
      console.log("Adjusting end date to " + $scope.startDate);
      $scope.endDate = $scope.startDate;
    }
  };
  </code></pre>
  
  <p>It is called from <code>ng-change</code> on the start date picker. Commenting it out makes the odd behaviour go away. But I don't understand why it would cause this issue. I need this function so that when the user has chosen a start date, the end date is automatically adjusted so that it is never before the start date.</p>
  
  <p>Complete code:</p>
  
  <p><strong>index.html</strong></p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test page&lt;/title&gt;
  
    &lt;link rel="stylesheet" href="bootswatch-dist/css/bootstrap.min.css"&gt;&lt;/link&gt;
    &lt;link rel="stylesheet" href="css/main.css"&gt;&lt;/link&gt;
  
    &lt;script&gt;
  var conf = {
    templates: {
      datepickModal: "script/angular/templates/datepickModal.html"
        }
  };
    &lt;/script&gt;
    &lt;script src="angular/angular.js" &gt;&lt;/script&gt;
    &lt;script src="jquery/dist/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script src="angular-animate/angular-animate.js"&gt;&lt;/script&gt;
    &lt;script src="angular-bootstrap/ui-bootstrap.js"&gt;&lt;/script&gt;
    &lt;script src="angular-bootstrap/ui-bootstrap-tpls.js"&gt;&lt;/script&gt;
    &lt;script src="script/angular/main.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body data-ng-app="mainApp"&gt;
      &lt;div id="main" ng-controller="testCtrl"&gt;
          &lt;button ng-click="openModal()"&gt;Launch modal&lt;/button&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>+function(angular, d3) {
    "use strict";
    var app = angular.module('mainApp', ['ui.bootstrap']);
  
  
    app.controller('datepickCtrl', ['$scope', '$rootScope', '$uibModalInstance', function($scope, $rootScope, $uibModalInstance) {
      $scope.startDate = new Date();
      $scope.endDate = new Date();
      $scope.startOpened = false;
      $scope.endOpened = false;
      $scope.name = '';
      $scope.mode = 'list';
      $scope.save = function () {
        alert("Saving dates start: " + $scope.startDate + " and end " +     $scope.endDate);
      };
      $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
      };
      $scope.toggleStartCalendar = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
  
        $scope.startOpened = !$scope.startOpened;
      };
      $scope.toggleEndCalendar = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
  
        $scope.endOpened = !$scope.endOpened;
      };
      $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1
      };
  
      $scope.setMode = function(mode) {
        $scope.mode = mode;
      }
  
      $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
      };
  
  
      $scope.adjustEndDate =  function() {
        if ($scope.endDate &lt; $scope.startDate) {
          console.log("Adjusting end date to " + $scope.startDate);
          $scope.endDate = $scope.startDate;
        }
      };
  
      $scope.adjustStartDate = function() {
        if ($scope.startDate &gt; $scope.endDate) {
          console.log("Adjusting start date to " + $scope.endDate);
          $scope.startDate = $scope.endDate;
        }
      };
  
    }]);
  
    app.controller('testCtrl', ['$scope', '$rootScope', '$uibModal',     function($scope, $rootScope, $uibModal) {
  
      $scope.openModal = function() {
        if (conf.suspended) {
          return;
        }
        var modalInstance = $uibModal.open({
          animation: true,
          templateUrl: conf.templates.datepickModal,
          controller: 'datepickCtrl',
          size: 'md'
        });
      };
    }]);
  }(window.angular, window.d3);
  </code></pre>
  
  <p><strong>modal.html</strong></p>
  
  <pre><code>&lt;div class="modal-header"&gt;
    &lt;h3 class="modal-title"&gt;Datepick tester&lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="modal-body"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-6"&gt;
              &lt;p class="input-group"&gt;
                &lt;label for="startdate"&gt;Start date&lt;/label&gt;&lt;p class="input-group"&gt;
                &lt;input type="text" class="form-control" uib-datepicker-popup ng-model="startDate" ng-change="adjustEndDate()" is-open="startOpened" ng-required="true" uib-datepicker-options="dateOptions" name="startdate" ng-click="toggleStartCalendar($event)"/&gt;
                &lt;span class="input-group-btn"&gt;
                  &lt;button type="button" class="btn btn-default" ng-click="toggleStartCalendar($event)"&gt;&lt;i class="glyphicon glyphicon-calendar"&gt;&lt;/i&gt;&lt;/button&gt;
                &lt;/span&gt;
              &lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="col-md-6"&gt;
            &lt;p class="input-group"&gt;
              &lt;label for="enddate"&gt;End date&lt;/label&gt;&lt;p class="input-group"&gt;
              &lt;input type="text" class="form-control" uib-datepicker-popup ng-model="endDate" is-open="endOpened" ng-change="adjustStartDate()" ng-required="true" uib-datepicker-options="dateOptions" name="enddate" ng-click="toggleEndCalendar($event)"/&gt;
              &lt;span class="input-group-btn"&gt;
                  &lt;button type="button" class="btn btn-default" ng-click="toggleEndCalendar($event)"&gt;&lt;i class="glyphicon glyphicon-calendar"&gt;&lt;/i&gt;&lt;/button&gt;
              &lt;/span&gt;
            &lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="modal-footer"&gt;
    &lt;button class="btn btn-primary" ng-click="save()"&gt;Save&lt;/button&gt;
    &lt;button class="btn btn-warning" ng-click="cancel()"&gt;Close&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Versions used</strong></p>
  
  <ul>
  <li>AngularJS: 1.4.8</li>
  <li>JQuery: 2.1.4</li>
  <li>UI-bootstrap: 1.2.4</li>
  </ul>