Ionic - AngularJS : ion-toggle doesn't update the model

13,304

I used ng-change to detect a change; and I am calling the function toggleChange() upon a change. So your ion-toggle will look like:

<ion-toggle ng-model="value" ng-change="toggleChange()">Test toggle</ion-toggle>

And your controller will change the $scope.value and hence you'll get the toggle's value from $scope.value:

.controller('ContactCtrl', function($scope, $interval) {
            $scope.value = false;
            console.log('ContactCtrl started');

            $scope.toggleChange = function() {
                if ($scope.value == false) {
                    $scope.value = true;
                } else
                    $scope.value = false;
                console.log('testToggle changed to ' + $scope.value);
            };
        }

Here's the Codepen for the same: http://codepen.io/keval5531/pen/LVYROp

Share:
13,304
noli
Author by

noli

Updated on June 14, 2022

Comments

  • noli
    noli almost 2 years

    I have a strange issue with the ion-toggle directive in Ionic framework.

    When using ion-toggle like this :

    <ion-toggle ng-model="testToggle">Test toggle</ion-toggle>
    

    JS:

    $scope.$watch('testToggle',function(value) {
        console.log('testToggle changed to '+value);
    })
    

    The controller doesn't receive any update at all.

    Here's the CodePen : http://codepen.io/anon/pen/jPOMqz

    You'll see that i've added an $interval that changes a binded variable to random in order to see that everything else works as expected

    Thank you very much :)