Search code examples
angularjscordovahybrid-mobile-app

Selected date from $cordovaDatePicker not being captured (ngCordova plugin)


I have the following code snippet in my application:

 ...
 $scope.fn.showDatePicker = function () {
            $cordovaDatePicker.show({
                    date: new Date(),
                    mode: 'date'
                },
                function (date) {
                    $scope.display.completionDate = $filter('date')(new Date(date));
                }
            );
        };
...

And the following HTML:

...
<input type="text" ng-model="displaycompletionDate" placeholder="Est. Completion Date" />
<button class="button button-dark button-small center-button" ng-click="fn.showDatePicker()">Click</button>
...

The native datepicker comes up fine but after I select the date, it doesn't seem to run the callback function. (or at least, my $scope variable is does not seem to be populated) I have only tested this on Android so far. The version of cordova I am using is 3.5.0-0.2.7. I am injecting the $cordovaDatePicker and get no errors.

Has anyone else encountered this problem?


Solution

  • I did some digging around and discovered the documentation on ngCordova is outdated. According to this commit log, we now get the entered date via a promise instead of a callback:

    $cordovaDatePicker.show({
        date: new Date(),
        mode: 'date'
    }).then(function (date) {
        $scope.display.completionDate = $filter('date')(new Date(date));
    });
    

    Hopefully this helps someone out there who was looking at ngCordova documentation.