[英]How to pass controller Function variable to Datepicker Directive?
I am new to Angularjs. 我是Angularjs的新手。 I have put 2 Datepickers.
我放了2个Datepickers。 I need that When I select date from first datepicker then the smaller dates from that date should be disabled in second Datepicker.
我需要当我从第一个Datepicker选择日期时,应在第二个Datepicker中禁用该日期之后的较小日期。 I am unable to set "minDate" for second Datepicker directive.
我无法为第二个Datepicker指令设置“ minDate”。
Below is my code : 下面是我的代码:
HTML Code : HTML代码:
<div ng-controller='TestController'>
<label>From Date :</label>
<input type="text" id="1" datepicker="" ng-init="variable=''"
ng-model="startDate" ng-change="test()" />
<label>To Date :</label>
<input type="text" id="2" callFuc="test()" datepicker1=""
ng-model="endDate" />
</div>
Js Code: Js代码:
app.controller('TestController', function($scope, $window) {
$scope.test = function() {
$scope.variable = $scope.startDate;
$window.alert("From date is" + $scope.variable);
};
});
app.directive('datepicker', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$('#1').datepicker({
minDate: 0,
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
}
};
});
app.directive('datepicker1', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
$('#2').datepicker({
minDate: scope.test(),
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
}
};
});
I am able to disable past dates in first Datepicker using minDate : "0"
. 我可以使用
minDate : "0"
禁用第一个Datepicker中的过去日期。
But I am unable to set minDate
properly for the second Datepicker. 但是我无法为第二个
minDate
正确设置minDate
。 I am not able to fetch selected date from the First Datepicker and pass it to "datepicker1" directive. 我无法从“第一个Datepicker”中获取选定的日期,并将其传递给“ datepicker1”指令。 Any help or suggestion will be appreciated.
任何帮助或建议,将不胜感激。
I have updated code. 我已经更新了代码。 Kindly review it.
请检查一下。
https://plnkr.co/edit/ohCFzpwLGgFPPH49jybq?p=preview https://plnkr.co/edit/ohCFzpwLGgFPPH49jybq?p=preview
The datepicker directive needs to watch for changes and update minDate
as needed: datepicker指令需要监视更改并根据需要更新
minDate
:
app.directive('datepicker1', function() {
return {
require: 'ngModel',
link: function(scope, el, attr, ngModel) {
scope.$watch(attr.minDate, function(value) {
el.datepicker({
minDate: value,
onSelect: function(dateText) {
scope.$apply(function() {
ngModel.$setViewValue(dateText);
});
}
});
});
}
};
});
Usage: 用法:
<label>To Date :</label>
<input type="text" min-date="variable" datepicker1
ng-model="endDate" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.