fragaria / angular-daterangepicker

Angular.js wrapper for dangrossman/bootstrap-daterangepicker
MIT License
519 stars 372 forks source link

apply.daterangepicker picker arg undefined #119

Closed ranieri82 closed 5 years ago

ranieri82 commented 8 years ago

In the eventHandler for apply.daterangepicker the picker argument is undefined

                $scope.datePickerOptions = {
                    showDropdowns: true,
                        locale: {
                                "format": "DD-MMM-YY"
                        },  
                        eventHandlers: {
                            //Selection event handler
                            'apply.daterangepicker': function(ev, picker) { 
                              //picker is undefined here............

                             }
                        },                                  
                        //default value is set to Yesterday to force selection on open
                        startDate: moment($scope.yesterdayDate), 
                        endDate: moment($scope.yesterdayDate),
                        drops: "up",
                        opens: "center" 
                    };  
umdstu commented 8 years ago

I'm experiencing the same issue. Let's keep the focus in this ticket as the other one, #87, is a different issue.

umdstu commented 8 years ago

@ranieri82 did you find a way around this at all?

karthikvu commented 8 years ago

Even I faced the picker=undefined issue, but I was able to get the dates this way :

      $scope.datePicker.options = {
                eventHandlers: {
                    'apply.daterangepicker': function(ev, picker) { 
                        $timeout(function(){
                            console.log($scope.datePicker.date.startDate.format('YYYY-MM-DD'));
                            console.log($scope.datePicker.date.endDate.format('YYYY-MM-DD'));
                        });
                    }
                }
      }
Albert91 commented 8 years ago

I also faced with picker=undefined.

Access to date is also available from ev param like this:

  $scope.datePicker.options = {
            eventHandlers: {
                'apply.daterangepicker': function(ev, picker) { 
                    $timeout(function(){
                        console.log(ev.model.startDate);
                        console.log(ev.model.endDate);
                    });
                }
            }
  }
mschaeuble commented 8 years ago

I was struggling with the same issue. As far as I could see, the problem is how the events are delegated. In the current implementation of angular-daterangepicker, event arguments are ignored and the event handlers defined via opts are called using $scope.$evalAsync, which executes the passed function with the current scope as an argument:

for eventType of opts.eventHandlers
        el.on eventType, (e) ->
          eventName = e.type + '.' + e.namespace
          $scope.$evalAsync(opts.eventHandlers[eventName])

Instead, I think the code should look more something like this:

for eventType of opts.eventHandlers
        el.on eventType, (e, arg) ->
          eventName = e.type + '.' + e.namespace
          $scope.$apply(opts.eventHandlers[eventName].call(this, e, arg));

What do you think?

Mr-Anonymous commented 7 years ago

I downloaded the today's version of angular datepicker with BS datepicker v2.1.24. I am also getting the error

Error: picker is undefined
loadConfig/config.eventHandlers["apply.daterangepicker"]

My event handler is like this:

eventHandlers: {
                      'apply.daterangepicker': function(ev, picker) {

                        var endDate = picker.endDate.format();
                        var startDate = picker.startDate.format();
                         ...
                        }
mschlitz-trux commented 4 years ago

This issue still exists today.