我有一个表格,其中我需要两个或多个日期字段来处理不同的事情。我尝试了Angular UI Bootstrap,当表单中只有1个日期字段时,它可以正常工作。但是如果我有多个日期字段并且我不知道更简单的方法来使它生效,它将停止工作。
这是我的HTML示例:
<label>First Date</label> <div class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" name="dt" ng-model="formData.dt" is-open="opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" /> <span class="input-group-btn"> <button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button> </span> </div> <label>Second Date</label> <div class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" name="dtSecond" ng-model="formData.dtSecond" is-open="opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" /> <span class="input-group-btn"> <button class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button> </span> </div>
我的JS是:
myApp.controller('DatePickrCntrl', function ($scope) { $scope.today = function() { $scope.formData.dt = new Date(); }; $scope.today(); $scope.showWeeks = true; $scope.toggleWeeks = function () { $scope.showWeeks = ! $scope.showWeeks; }; $scope.clear = function () { $scope.dt = null; }; // Disable weekend selection $scope.disabled = function(date, mode) { return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) ); }; $scope.toggleMin = function() { $scope.minDate = ( $scope.minDate ) ? null : new Date(); }; $scope.toggleMin(); $scope.open = function($event) { $event.preventDefault(); $event.stopPropagation(); $scope.opened = true; }; $scope.dateOptions = { 'year-format': "'yy'", 'starting-day': 1 }; $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate']; $scope.format = $scope.formats[0]; });
我基于此处的示例实施。我这里的问题是:
1)单击日期字段之一时,弹出的日期选择器被弄乱了,似乎在1中显示了2个日期选择器。
2)当我删除is-open="opened"属性时,弹出窗口似乎正常工作。但是,如果没有is-open="opened",则ng- click="open($event)for按钮不起作用。
is-open="opened"
ng- click="open($event)
3)由于每个日期字段都有不同的ng模型,因此我无法为任何日期字段设置默认日期,但第一个带有 ng-model="formData.dt"
ng-model="formData.dt"
我能想到的解决此问题的唯一长途方法是为每个日期字段分离控制器。
我想知道其他人在使用Angular UI Bootstrap时如何以一种形式本身实现多个日期字段。
我有一种形式的30个控制器,没问题。如果需要在ng-repeat上使用相同的概念。
<label>First Date</label> <div class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" name="dt" ng-model="formData.dt" is-open="datepickers.dt" datepicker-options="dateOptions" ng-required="true" close-text="Close" /> <span class="input-group-btn"> <button class="btn btn-default" ng-click="open($event,'dt')"> <i class="glyphicon glyphicon-calendar"></i></button> </span> </div> <label>Second Date</label> <div class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" name="dtSecond" ng-model="formData.dtSecond" is-open="datepickers.dtSecond" datepicker-options="dateOptions" ng-required="true" close-text="Close" /> <span class="input-group-btn"> <button class="btn btn-default" ng-click="open($event,'dtSecond')"> <i class="glyphicon glyphicon-calendar"></i></button> </span> </div> myApp.controller('DatePickrCntrl', function ($scope) { $scope.datepickers = { dt: false, dtSecond: false } $scope.today = function() { $scope.formData.dt = new Date(); // ***** Q1 ***** $scope.formData.dtSecond = new Date(); }; $scope.today(); $scope.showWeeks = true; $scope.toggleWeeks = function () { $scope.showWeeks = ! $scope.showWeeks; }; $scope.clear = function () { $scope.dt = null; }; // Disable weekend selection $scope.disabled = function(date, mode) { return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) ); }; $scope.toggleMin = function() { $scope.minDate = ( $scope.minDate ) ? null : new Date(); }; $scope.toggleMin(); $scope.open = function($event, which) { $event.preventDefault(); $event.stopPropagation(); $scope.datepickers[which]= true; }; $scope.dateOptions = { 'year-format': "'yy'", 'starting-day': 1 }; $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate']; $scope.format = $scope.formats[0]; }); // ***** Q2 ***** somemodel can be just an array [1,2,3,4,5] <div ng-repeat="o in somemodel"> <label>Date Label</label> <div class="input-group"> <input type="text" class="form-control" datepicker-popup="{{format}}" name="dt{{o}}" ng-model="datepickers.data[o]" is-open="datepickers.isopen[o]" datepicker-options="datepickers.option" ng-required="true" close-text="Close" /> <span class="input-group-btn"> <button class="btn btn-default" ng-click="open($event,o)"> <i class="glyphicon glyphicon-calendar"></i></button> </span> </div> </div> myApp.controller('DatePickrCntrl', function ($scope) { $scope.datepickers = { data: {}, options: { 'year-format': "'yy'", 'starting-day': 1 }, isopen: {} } $http.get("get/data/for/some/model", function(result) { $scope.somemodel = result; for (var i = 0; i < result.length; i++) { $scope.datepickers.isopen[result] = false; $scope.datepickers.data[result] = new Date(); //set default date. } }); // fill in rest of the function });