fragaria / angular-daterangepicker

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

singleDatePicker throws TypeError: Cannot read property 'startDate' of undefined #150

Closed xoloinc closed 8 years ago

xoloinc commented 8 years ago

When you initiate the picker with an null value for date, the formatter push iteration fails, throwing a
TypeError: Cannot read property 'startDate' of undefined

The if statement first checks if singleDatePicker && date is set, if not, next step checks for objValue.startDate only, which makes the error to occur caused by objValue being a null value.

This should be fixed by adding a check for both objValue && objValue.startDate in the clause.

  if opts.singleDatePicker and objValue
        f(objValue)
      else if objValue.startDate
        [f(objValue.startDate), f(objValue.endDate)].join(opts.locale.separator)
      else ''

to

   if opts.singleDatePicker and objValue
        f(objValue)
      else if objValue and objValue.startDate
        [f(objValue.startDate), f(objValue.endDate)].join(opts.locale.separator)
      else ''

or am I missing something?

xoloinc commented 8 years ago

just saw Add back check for undefined model value #136 solves this

oles-bolotniuk commented 6 years ago

As #136 is not merged yet in 2o18

In our project I fixed this by exposing model date and options objects to the controller scope:

View:

<div
  date-range-picker
  ng-model="date"
  options="options"
></div>

Controller:

$scope.options = {
  showDropdowns: true,
  opens: 'left',
  drops: 'down',
  applyClass: 'btn-primary'
};

$scope.date = {
  startDate: moment(),
  endDate: moment()
};