Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带有AngularJS的日期范围选择器指令_Javascript_Angularjs - Fatal编程技术网

Javascript 带有AngularJS的日期范围选择器指令

Javascript 带有AngularJS的日期范围选择器指令,javascript,angularjs,Javascript,Angularjs,我在日期范围选择器指令和自定义和范围方面遇到问题,我已将原始指令修改为: (function (angular) { 'use strict'; angular.module('ngBootstrap', []).directive('input', function ($compile, $parse) { return { restrict: 'E', require: 'ngModel', link: function ($scop

我在日期范围选择器指令和自定义和范围方面遇到问题,我已将原始指令修改为:

(function (angular) {
'use strict';

angular.module('ngBootstrap', []).directive('input', function ($compile, $parse) {
    return {
        restrict: 'E',
        require: 'ngModel',
        link: function ($scope, $element, $attributes, ngModel) {
            if ($attributes.type !== 'daterange') return;

            var options = {};
            options.format = $attributes.format || 'YYYY-MM-DD';
            options.separator = $attributes.separator || ' - ';
            options.minDate = $attributes.minDate && moment($attributes.minDate);
            options.maxDate = $attributes.maxDate && moment($attributes.maxDate);
            options.dateLimit = $attributes.limit && moment.duration.apply(this, $attributes.limit.split(' ').map(function (elem, index) { return index === 0 && parseInt(elem, 10) || elem; }) );
            options.ranges = $attributes.ranges && $parse($attributes.ranges)($scope);

            function format(date) {
                return date.format(options.format);
            }

            function formatted(dates) {
                return [format(dates.startDate), format(dates.endDate)].join(options.separator);
            }

            ngModel.$formatters.unshift(function (modelValue) {
                if (!modelValue) return '';
                return modelValue;
            });

            ngModel.$parsers.unshift(function (viewValue) {
                return viewValue;
            });

            ngModel.$render = function () {
                if (!ngModel.$viewValue || !ngModel.$viewValue.startDate) return;
                $element.val(formatted(ngModel.$viewValue));
            };

            $scope.$watch($attributes.ngModel, function (modelValue) {
                if (!modelValue || (!modelValue.startDate)) {
                    ngModel.$setViewValue({ startDate: moment().startOf('day'), endDate: moment().startOf('day') });
                    return;
                }
                $element.data('daterangepicker').startDate = modelValue.startDate;
                $element.data('daterangepicker').endDate = modelValue.endDate;
                $element.data('daterangepicker').updateView();
                $element.data('daterangepicker').updateCalendars();
                $element.data('daterangepicker').updateInputText();
            });

            $element.daterangepicker(options, function(start, end) {
                $scope.$apply(function () {
                    ngModel.$setViewValue({ startDate: start, endDate: end });
                    ngModel.$render();
                });
            });         
        }
    };
});

})(angular);
允许以下声明:

<date-range-picker ng-model="dateRange"
                   class="pull-right"
                   style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc"
                   min-date="{{minDate}}"
                   max-date="{{maxDate}}"
                   ranges="{'Last 7 Days': [moment().subtract('days', 6), moment()],
                            'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')],
                            'Last 3 Months': [moment().subtract('month', 3).startOf('month'), moment().subtract('month', 1).endOf('month')]}">
    <i class="fontello-icon-calendar-alt-1"></i>
    <span>....</span>


    <b class="caret" style="margin-top: 8px"></b>
</date-range-picker>

选择一个间隔是什么意思?问题到底是什么?问题是什么?预定义的范围是链接不起作用,它们没有正确选择日期范围,所有三个范围都在下拉列表中突出显示,我不知道原因是绑定还是其他原因。找到问题并更新了问题,范围和ng模型属性需要由控制器支持,如原始组件URL上所示:我遇到了与您描述的完全相同的问题:默认情况下,日期范围都突出显示,并且都表示相同的日期范围(今天),尽管它们变化很大(今年、上月等)我通过在模型中提供dateRange和dateRange.ranges正确地支持了该组件,但似乎仍然无法修复它。你到底在控制器代码中做了什么更改?我似乎找不到区别。谢谢
<date-range-picker ng-model="dateRange"
                               class="pull-right"
                               style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc"
                               ng-model="dateRange"
                               ranges="dateRange.ranges"
                               >
                <i class="fontello-icon-calendar-alt-1"></i>
                <span></span>
                <b class="caret" style="margin-top: 8px"></b>
</date-range-picker>
((angular) ->
  "use strict"
  angular.module("directives.daterangepicker", []).directive "dateRangePicker", ($compile, $parse) ->
    restrict: "E"
    require: "ngModel"
    link: ($scope, $element, $attributes, ngModel) ->
      format = (date) ->
        date.format options.format
      formatted = (dates) ->
        [format(dates.startDate), format(dates.endDate)].join options.separator
      options = {}
      options.format = $attributes.format or "MMM D, YYYY"
      options.separator = $attributes.separator or " - "
      options.opens = $attributes.opens or "left"
      options.minDate = $attributes.minDate and moment($attributes.minDate)
      options.maxDate = $attributes.maxDate and moment($attributes.maxDate)
      options.dateLimit = $attributes.limit and moment.duration.apply(this, $attributes.limit.split(" ").map((elem, index) ->
        index is 0 and parseInt(elem, 10) or elem
      ))
      options.ranges = $attributes.ranges and $parse($attributes.ranges)($scope)
      ngModel.$formatters.unshift (modelValue) ->
        return ""  unless modelValue
        modelValue

      ngModel.$parsers.unshift (viewValue) ->
        viewValue

      ngModel.$render = ->
        return  if not ngModel.$viewValue or not ngModel.$viewValue.startDate
        $element.find("span").html(formatted(ngModel.$viewValue))

      $scope.$watch $attributes.ngModel, (modelValue) ->
        if not modelValue or (not modelValue.startDate)
          ngModel.$setViewValue
            startDate: moment().startOf("day")
            endDate: moment().startOf("day")

          return
        $element.data("daterangepicker").startDate = modelValue.startDate
        $element.data("daterangepicker").endDate = modelValue.endDate
        $element.data("daterangepicker").minDate = modelValue.minDate
        $element.data("daterangepicker").maxDate = modelValue.maxDate
        $element.data("daterangepicker").updateView()
        $element.data("daterangepicker").updateCalendars()
        #$element.data("daterangepicker").updateInputText()

      $element.daterangepicker options, (start, end) ->
        alert "$apply#{moment(start).toDate()}"
        alert options.maxDate
        $scope.$apply ->
          ngModel.$setViewValue
            startDate: start
            endDate: end

          ngModel.$render()



) angular