0

I'm having difficulties when a checkbox is toggled. By default the checkbox is checked because a value from local storage is set to yes. when the checkbox is toggled the local storage value changes to no and should pop up. But with my script even when the value is set to no, the alert keeps popping up

HTML

<li class="item item-toggle">
     Following
     <label class="toggle toggle-balanced">
       <input type="checkbox" ng-model="set_following" ng-checked="following == 'yes'" ng-change="following()">
       <div class="track">
         <div class="handle"></div>
       </div>
     </label>
  </li>

JS

    $scope.following=function(){
    $scope.push_following= localStorage.getItem('push_following')
    if ($scope.push_following=='yes') {
    localStorage.setItem("push_following",'no');
    var confirmPopup = $ionicPopup.confirm({
    title: 'Following Notifications',
    template: 'Sure you want to stop following User?',
    cancelText: 'No',
    okText: 'Yes'
     });
     confirmPopup.then(function(res) {
    if(res) {
    event.preventDefault();
    $http.post("http://localhost/myapp/scripts/follow.php",
    {'email':$scope.email}).success(function(data){
    }).error(function(error){
    //console.error(error);

    });
    } else {
    localStorage.setItem("push_following",'yes');
       }
})
     } 

    }
Ahmer Khan
  • 747
  • 1
  • 10
  • 31
user6579134
  • 749
  • 3
  • 10
  • 35

1 Answers1

0

Change (==) to (===) on your view :

<input type="checkbox" ng-model="set_following" ng-checked="following === 'yes'" ng-change="following()">

Change (==) to (===) on your controller :

if ($scope.push_following === 'yes') {

Or use angular.equals - https://docs.angularjs.org/api/ng/function/angular.equals

More information Which equals operator (== vs ===) should be used in JavaScript comparisons?

Community
  • 1
  • 1
daan.desmedt
  • 3,752
  • 1
  • 19
  • 33