2016-05-29 21 views
1

AngularJsを使用して、Datepickerの特定の日付を動的に無効にしたい。ここで私は何をしました。datepickerで特定の日付を動的に無効にする

var app = angular.module('plunker', ['ui.bootstrap']); 
 

 
app.controller('MainCtrl', function($scope, $q) { 
 

 

 

 
    $scope.date1 = new Date(); 
 
    $scope.date2 = new Date(); 
 
    $scope.date2.setDate($scope.date2.getDate() - 1); 
 

 

 

 
    var currentDay = new Date(); 
 

 

 
    $scope.isDateDisabled = function(date, mode) { 
 
    /*console.log("date=="+date.getTime()); 
 
    console.log("currentdate"+$scope.date2.getTime());*/ 
 
    if (date.getDate() === $scope.date2.getDate()) { 
 
     return true; 
 
    } else { 
 
     return false; 
 
    } 
 

 
    }; 
 
});
/* Put your css in here */ 
 

 
.padTop { 
 
    padding-top: 20px; 
 
}
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script> 
 
    document.write('<base href="' + document.location + '" />'); 
 
    </script> 
 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet" /> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.25/angular.js" data-semver="1.2.25"></script> 
 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.min.js"></script> 
 
    <script src="app.js"></script> 
 
</head> 
 

 
<body ng-controller="MainCtrl"> 
 
    <div class="container padTop"> 
 

 

 
    <div> 
 
     <div style="display:inline-block; min-height:290px;"> 
 
     <datepicker ng-model="date1" min-date="minDate" show-weeks="true" date-disabled="isDateDisabled(date,mode)" class="well well-sm"></datepicker> 
 
     </div> 
 
     <div style="display:inline-block; min-height:290px;"> 
 
     <datepicker ng-model="date2" min-date="minDate" show-weeks="true" class="well well-sm"></datepicker> 
 
     </div> 
 
    </div> 
 

 

 

 
    <p class="padTop">date1:</p> 
 
    <pre> 
 
{{ date1 | date : 'dd/MM/yyyy' }} 
 

 
{{ date2 | date : 'dd/MM/yyyy' }} 
 
</pre> 
 

 
    </div> 
 

 

 
</body> 
 

 
</html>

なお、第2の日付ピッカーで選択された第一日付ピッカー、日付を無効にされました。しかし、2番目のdatepickerで日付を変更しても、それは反映されません(つまり、1番目のdatepickerでは無効になりません)。とにかくこのケースを処理するには?

答えて

0

AFAIKこれを行うにはクリーンな方法はありませんが、minDate属性を変更するとdatepickerが更新されます。コントローラにこれを追加すると、動作します。

$scope.$watch("date2", function (newValue, oldValue) { 
      $scope.minDate= new Date(); 
      $timeout(function() { 
       $scope.minDate = null; 
      }, 0); 
}); 
関連する問題