【发布时间】:2016-11-19 11:52:21
【问题描述】:
我正在尝试编写 jquery 以使用隐藏输入区域在文本单击时打开日期选择器。这在多个地方也是必需的。
以下是我的html。
<div class="date-format">
<input class="hidden-date-picker" id="hidden-date1" type="hidden" ng-model="date1"/>
<a href="" class="date-picker" id="date1">{{date1 | date: 'dd MMM yyyy a'}}</a>
</div>
<div class="date-format">
<input class="hidden-date-picker" id="hidden-date2" type="hidden" ng-model="date2"/>
<a href="" class="date-picker" id="date2">{{date2 | date: 'dd MMM yyyy a'}}</a>
</div>
以下是 javascript -
var app = angular.module('index-app', []);
app.controller('index-controller', function($scope) {
$scope.date1 = new Date(2016, 0, 1);
$scope.date2 = new Date();
$('.hidden-date-picker').each(function() {
$(this).datepicker({
changeYear: 'true',
changeMonth: 'true',
startDate: '07/16/1989',
firstDay: 1,
onSelect: function(dateText, inst) {
console.log($scope.$eval($(this).attr('ng-model')));
$scope.$eval($(this).attr('ng-model')).setTime(Date.parse(dateText));
console.log($scope.date1);
}
});
});
$('.date-picker').each(function() {
$(this).click(function (e) {
$('#hidden-' + $(this).attr('id')).datepicker('show')
e.preventDefault();
});
});
});
从控制台输出中可以清楚地看出,在从日历中选择日期时正在修改 $scope.date1。但是这种变化并没有反映在 html 方面。
【问题讨论】:
-
使用 $scope.$apply() 后,我能够解决问题。但是,如果我在其他地方尝试 。它不会工作。
标签: javascript jquery angularjs datepicker