Ion-toggle not updating model


#1
<ion-toggle ng-model=Obj.isDate ng-change="dateChange()"> Choose date <span class="item-note" ng-show="Obj.isDate">
    On : {{Obj.Date | date:'mediumDate'}}
</span></ion-toggle>




$scope.dateChange = function () {

       

        if ($scope.Obj.isDate)
        {
            var options = {
                date: new Date(),
                mode: 'date',
                minDate: new Date() - 10000,
                allowOldDates: false,
                allowFutureDates: true,
                doneButtonLabel: 'DONE',
                doneButtonColor: '#F2F3F4',
                cancelButtonLabel: 'CANCEL',
                cancelButtonColor: '#000000',
                x: "400",
                y: "500",
                androidTheme: window.datePicker.ANDROID_THEMES.THEME_DEVICE_DEFAULT_DARK
            };
            window.plugins.datePicker.show(options, function (date) {
                if (angular.isDefined(date))
            {
                $scope.Obj.Date = date;
            } else {
                 $scope.Obj.Date = new Date();
            }
            }, function (err) {
                $scope.Obj.isDate = false; <===== ISSUE HERE
            })
        } else {
            $scope.Obj.Date = null;
        }

    }

Here when user is pressing cancel button in date picker I need to turn off toggle button. But this code is not working. Toggle button model is not updating.


#2

try to wrap this line in $timeout-function, because the click and the whole date picker is running outside of your angularjs-context. To get the values correct updated you have to wrap them in a $timeout.

}, function (err) {
  $timeout(function () {
    $scope.Obj.isDate = false; <===== ISSUE HERE
  });
})

#3

Thanks bengtler. It did work. I was trying something else… I was triggering the click button. But your solution is better…