正在将日期范围选择器转换为角度指令

Converting daterangepicker to angular directive

本文关键字:指令 转换 选择器 日期 范围      更新时间:2024-06-12

我正在尝试将日期范围选择器插件封装到角度指令中。尽管我通过在回调函数中提醒所选日期来实现它,但我似乎无法将所选日期保存到$scope或更新ng模型。我添加了一条注释"//CALLBACK of daterangepicker",这样任何看到它的人都可以在下面的代码中轻松找到它。我希望有更多经验的人能够阐明如何实现这一目标。

HTML(调用指令):

<input id="date-range-picker" class="form-control" type="text" 

ng model="date"时间记录器日期范围选择器/>

角度指令:

module.directive('timeRecorderDateRangePicker', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModelCtrl) {
            element.daterangepicker({
                startDate: moment.utc().subtract(7, 'days'),
                endDate: moment.utc(),
                minDate: '01/01/2014',
                maxDate: moment.utc(),
                dateLimit: {
                    days: 365
                },
                showDropdowns: false,
                showWeekNumbers: true,
                timePicker: false,
                ranges: {
                    'Today': [moment.utc(), moment.utc()],
                    'Yesterday': [moment.utc().subtract(1, 'days'), moment.utc().subtract(1, 'days')],
                    'Last 7 Days': [moment.utc().subtract(6, 'days'), moment.utc()],
                    'Last 30 Days': [moment.utc().subtract(29, 'days'), moment.utc()],
                    'This Month': [moment.utc().startOf('month'), moment.utc().endOf('month')],
                    'Last Month': [moment.utc().subtract(1, 'month').startOf('month'), moment.utc().subtract(1, 'month').endOf('month')]
                },
                opens: 'right',
                format: 'MMMM D, YYYY',
                separator: ' to ',
                buttonClasses: ['btn btn-default'],
                locale: {
                    applyLabel: 'Apply',
                    cancelLabel: 'Cancel',
                    fromLabel: 'From',
                    toLabel: 'To',
                    customRangeLabel: 'Custom',
                    daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
                    monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
                    firstDay: 1
                }
            }, function(start, end, label) {
                // CALLBACK of daterangepicker
                alert('Callback!!!');
            }).prev().on('click', function() { // makes calendar icon click work
                $(this).next().focus();
            });
        }
    };
});

您可以使用jqlite在Angularjs项目中使用dangrossman的bootstrap日期范围选择器,试试这个

在HTML 中

<input type="text" id="daterange" />

然后使用jqlite 将选择器附加到要触发它的元素

 angular.element('#daterange').daterangepicker({
        timePicker: true,
        timePickerIncrement: 30,
        locale: {
            format: 'MM/DD/YYYY h:mm A'
        }
    });

您也可以使用事件

    angular.element('#daterange').on('apply.daterangepicker', function(ev, picker) {
      //do something, like calling a function
      $scope.doSomething(picker.startDate, picker.endDate);
    });

您可以将日期选择器转换为指令,并使模型更新如下:

angular.module('DatePicker', [])
.directive('datepicker', function() {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, element, attrs, ngModelCtrl) {
        element.datepicker({
          format: 'dd/mm/yy',
          onSelect: function(date) {
            ngModelCtrl.$setViewValue(date),
              scope.$apply();
          }
        });
      }
    }
  });