katemihalikova / ion-datetime-picker

Date and/or time picker for awesome Ionic framework
MIT License
168 stars 101 forks source link

I want to set startTime and add a callBack.How can i do? #19

Closed huangfang2015 closed 8 years ago

huangfang2015 commented 8 years ago

I want to set startTime and add a callBack.How can i do?

katemihalikova commented 8 years ago

You should use the same as with any other ng-model-enabled input.

<div ion-datetime-picker ng-model="datetime" ng-change="onChange()">{{datetime}}</div>
$scope.datetime = new Date(); // set start date and time
$scope.onChange = function() {}; // set callback
huangfang2015 commented 8 years ago

Thanks.

gqferreira commented 8 years ago

I could not make it work properly. My code:

app.js var app = angular.module('app', ['ionic', 'ionic.service.core', 'ionic.service.analytics', 'ngMessages', 'ngCordova', 'ionic.utils', 'app.controllers', 'app.routes', 'app.services', 'app.directives', 'angular-md5', 'ion-datetime-picker']);

controllers.js

.controller('MyController', function ($scope, $ionicPopup, AuthService, $state, $ionicLoading, $interval) {
   //Initial value
    $scope.datetime = new Date();

    //Event when confirm datetime
    $scope.datetimepickerEvent = function (){
                alert($scope.datetime);
            };

    //Function called before calendar show
    $scope.updateDatetime = function () {
                alert("I am working");
                $scope.datetime = new Date();
                alert($scope.datetime); // The datetime really update.
            };

});

home.html <div ion-datetime-picker ng-model="datetime" ng-change="datetimepickerEvent()" ng-click="updateDatetime()">{{datetime}}</div>

Problem After confirm datetime, the $scope.datetime doesn't show the same value in home.html and controllers.js, my function updateDatetime() really update datetime but can't update the datetime used by calendar. If I press Cancel button, this does not happen. If I confirm datetime in calendar and I need reopen it, the current datetime is outdated.

Is this a bug? Thank you for your attention. Gustavo Ferreira.

katemihalikova commented 8 years ago

This is not a bug, but a lack of knowledge about how angular and javascript work.

Please see #14 and this demo made from your code to understand why it does not work for you.

gqferreira commented 8 years ago

Ok, thank you.