0

我一辈子都无法让变量“开始”和“结束”返回到我在控制器中的函数。

l() == 控制台.log()

我试图把这个:http ://www.dangrossman.info/2012/08/20/a-date-range-picker-for-twitter-bootstrap/

对此的指令:https ://github.com/clouddueling/angularjs-common

控制器.js

ontrollers.controller('PatientsCtrl', function($scope, $http, $location, $cookies, Popup, Value, Users, User, System){
    $scope.loadUserSystems = function(start, end) {
        l(start);
        l(end);

        return;
        $scope.activeUser = this.user ? this.user : $scope.activeUser;
        User.systems($scope.activeUser.id, start, end).success(function(data){
            $scope.activeSystems = data.systems;
        });
    }


    $scope.dateRangeOptions = {
      ranges: {
                'Today': [new Date(), new Date()],
                'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
                'Last 7 Days': [moment().subtract('days', 6), new Date()],
                'Last 30 Days': [moment().subtract('days', 29), new Date()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
      },
      format: 'MM/DD/YYYY',
      separator: ' to ',
      startDate: moment().subtract('days', 29),
      endDate: new Date(),
      minDate: '01/01/2012',
      maxDate: '12/31/2013',
      locale: {
          applyLabel: 'Submit',
          fromLabel: 'From',
          toLabel: 'To',
          customRangeLabel: 'Custom Range',
          daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr','Sa'],
          monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
          firstDay: 1
      },
      showWeekNumbers: true,
      buttonClasses: ['btn-danger'],
      dateLimit: false
   }
});

部分.html

        <div class='btn'>
            <i class='icon-calendar'></i>
            <span
                date-range-picker
                options='dateRangeOptions'
                change='loadUserSystems(start, end)'></span>
        </div>

指令.js

directives.directive('dateRangePicker', function ($parse) {
  return {
    restrict: 'A',
    scope: {
      options: '=',
    },
    template: "<span></span> <b class='caret'></b>",
    link: function (scope, element, attr) {
      $(element).daterangepicker(scope.options, function(start, end){
        $(element).find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        // Retrieve the callback function
        var fn = $parse(attr.change);

        scope.$apply(function () {
          fn(scope, { start: start, end: end });
        });
      });

      $(element).find('span').html(moment().format('MMMM D, YYYY') + ' - ' + moment().format('MMMM D, YYYY'));
    }
  };
});
4

2 回答 2

1

试试这个:

directives.directive('dateRangePicker', ['$window', function ($window) {
  return {
    restrict: 'A',
    scope: {
      options: '=',
      onChange: '&change'
    },
    template: "<span></span> <b class='caret'></b>",
    link: function (scope, element, attr) {
      $(element).daterangepicker(scope.options, function(start, end){
        $(element).find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        scope.$apply(function () {
          scope.onChange({ start: start, end: end });
        });
      });

      $(element).find('span').html($window.moment().format('MMMM D, YYYY') + ' - ' + $window.moment().format('MMMM D, YYYY'));
    }
  };
}]);

此外,上述假设moment()是在窗口上。您可以在此处找到有关在父范围的上下文中执行表达式的更多信息。

编辑修复onChange调用。

于 2013-06-04T00:20:10.130 回答
0

多亏了 rtcherry 的解决方案,我才能够做几个模组来让它对我有用。

供应商:

  • Moment.js
  • jQuery

指令.js

directives.directive('dateRangePicker', ['$window', function ($window) {
  return {
    restrict: 'A',
    scope: {
      options: '=',
      change: '&'
    },
    template: "<span></span><b class='caret'></b>",
    link: function (scope, element, attr) {
      $(element).daterangepicker(scope.options, function(start, end){
        $(element).find('span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));

        scope.$apply(function () {
          scope.change({ start: start, end: end });
        });
      });

      $(element).find('span').html($window.moment().format('MMMM D, YYYY') + ' - ' + $window.moment().format('MMMM D, YYYY'));
    }
  };
}]);

控制器.js

  $scope.loadUserSystems = function(start, end) {
    start = start.format("YYYY-MM-DD");
    end = end.format("YYYY-MM-DD");

    $scope.activeUser = this.user ? this.user : $scope.activeUser;
    if (!$scope.activeUser)
      $scope.activeUser = $scope.users[0];

    User.systems($scope.activeUser.id, start, end, $scope.systemType).success(function(data){
      $scope.activeSystems = data.systems;
    });
  }

部分.html

  <div
    class='btn'
    date-range-picker
    change='loadUserSystems(start, end)'
    options='dateRangeOptions'></div>
于 2013-06-04T02:12:16.347 回答