Ionic Toggle Value

Hi guys,

anyone has an idea how to get the toggle value (true or false) in angularjs?

sample.html

<ion-toggle ng-model="addlist.nego" toggle-class="toggle-balanced">Negotiable</ion-toggle>

controller.js

console.log($scope.addlist.nego);

got a log undefined in the console log.

What is the correct way to get the value

1 Like

You might need to declare

$scope.addlist={}
first

hi @nabinkumarn

i have include $scope.addlist={}

but still the same

I think you can initialize $scope.addlist.nego =true; and keep the $scope.addlist.negoin ng-changeto see if the value change. Main thing is initializing first.

where to initialize for the first time?

html
<ion-toggle ng-model="a.airplaneMode" ng-checked="a.airplaneMode" ng-change="change()"toggle-class="toggle-calm">Airplane Mode</ion-toggle>

In the controller.
$scope.a = {};
$scope.a.airplaneMode = true;
$scope.change = function() {
console.log($scope.a.airplaneMode);
}

this worked for me.

hi @nabinkumarn

Using above code, everytime i click on the save button, the toggle will keep β€œON” even i have choose to close the toggle

can you look at this codepen
http://codepen.io/nabinkumarn/pen/jWZoXo

Its working

Where are you saving? Can you show how it is called? If you need it for to save it you can store it in localstorage and call value from localstorage in every initialize.

Figured it out, i have $scope inside my controller, just now i am initializing the toggle in the $scope not above the scope, now move it to before the scope and its work.

Thank you

1 Like