0
我有一個窗體內的兩個datepicker。當用戶選擇第一個日期時,第二個日期選擇器的最小和最大日期應該更新(從選擇起一年)。但在我的情況下,最初和最大日期只更新一次,當我第一次在第一個日期選擇器中選擇日期。但是如果我改變了選擇,第二個日期選擇器沒有用改變的值修改。不能在另一個日期選擇器中更改datepicker參數?
HTML:
<div class="form-group">
<label>Model Production Date</label>
<input date-picker1 type="text" class="form-control" placeholder="Production Date" ng-model="productionDate">
</div>
<div class="form-group">
<label>Next Review Date</label>
<input date-picker2 type="text" class="form-control" placeholder="Next Review Date" ng-model="nextReviewDate">
</div>
JS:
.directive('datePicker1', function() {
return function (scope, element, attrs) {
element.datepicker({
format: "yyyy/mm/dd",
todayHighlight: true,
//startDate: stDate || new Date(),
//endDate:enDate || new Date(),
autoclose: true
});
scope.$watch('productionDate', function (newVal, oldVal) {
if(newVal){
scope['productionDate'] = newVal;
}
else{
return;
}
});
};
})
.directive('datePicker2', function() {
return {
restrict: 'A',
link: linker,
}
function linker(scope, $element, attrs) {
scope.$watch('productionDate', function (newVal, oldVal) {
console.log('productionDate===='+scope.productionDate);
splittedDateString = scope.productionDate.split('/');
stDate = new Date(splittedDateString[0], splittedDateString[1]-1, splittedDateString[2]);
enDate = new Date(stDate.getFullYear()+1, splittedDateString[1]-1, splittedDateString[2]);
console.log("Start Date = "+stDate);
console.log("End Date = "+enDate);
$element.datepicker({
format: "yyyy/mm/dd",
todayHighlight: true,
startDate: stDate || new Date(),
endDate:enDate || new Date(),
autoclose: true
});
});
};
})
每次我可以看到改變了stDate
和enDate
價值,但第二個日期選擇開始日期和結束日期未更新。我被困了2天。請幫忙
嗨,至極腳本你使用渲染你的答案日期選擇器? – Jaay
你介意提供一個小提琴嗎? –
將'$ element.datepicker ...'包裹在'$ scope。$ evalAsync(function(){// date picker});' –