简体   繁体   中英

angularjs - Popup alert if value equal to x

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');
       }
})
     } 

    }

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM