lundi 30 mai 2016

Change checkbox value with request status AngularJs

I have this situation, in my app (developed with Ionic+AngularJs) there is a checkbox element, when you click on it an HTTP-request is sended and what I want is, depending on the result of that request, change the check status of the checkbox.

Now, what is happening is, whatever it is the request status, the checkbox changes its check status.

my index.html has this

 <ion-side-menu-content>
      <ion-nav-bar class="bar-dark">

        <ion-nav-buttons side="left">
          <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
          </button>
        </ion-nav-buttons>
      </ion-nav-bar>

      <ion-nav-view name="menuContent"></ion-nav-view>
    </ion-side-menu-content> 

    <ion-side-menu side="left">
      <ion-header-bar class="bar-dark menu-bar">
        <h1 class="title"> Categorías </h1>
      </ion-header-bar>
      <ion-content>
        <ul class="list">
          <a href="#/home" class="item" menu-close>Inicio</a>
          <a href="#/planesEstudio" class="item" menu-close>Planes de Estudio</a>
          <a href="#/calendarioAcademico" class="item" menu-close>Calendario académico</a>
          <a href="#/page/Finales" class="item" menu-close>Finales</a>
          <a href="#/news" class="item" menu-close> Noticias </a>
          <a href="" ng-controller="qrController" on-touch="scanBarcode()" class="item" menu-close> Aulas </a>
     <ion-checkbox class="item item-checkbox-right checkbox-circle" ng-model="notificationEnable" ng-checked="notificationEnable" ng-change="pushNotificationChange()">
              Activar notificaciones
          </ion-checkbox>
        </ul>
      </ion-content>
    </ion-side-menu>

  </ion-side-menus>

and my app.js (which manage this):

  $scope.pushNotificationChange = function(){

      if(localStorage.getItem("notificationEnable")=="true"){
        $cordovaToast.show("Desactivando notificaciones...",'short','bottom');
        unregistration();
      }
      else{
        $cordovaToast.show("Activando notificaciones...",'short','bottom');
        intentandoRegistrarse=true;
        $scope.register();
      }
  }
  $scope.register = function () {
      var config = null;
      config = { "senderID": "674717386103" };

      request = $http({         //hacemos este request para testear la conectividad al backend
                method: "GET",
                url: API+"/qrCode/1",
              });
    request.success(function (){$cordovaPush.register(config).then(function (result) {
                                          $scope.registerDisabled=true;
                                          if (intentandoRegistrarse==true){
                                                    $cordovaToast.show("Notificaciones activadas",'long','bottom');
                                                    intentandoRegistrarse=false;
                                            };
                                }, 
                                function (err) {
                                        console.log("Register error " + err);
                                });
    });

    request.error(function(){
                    if (intentandoRegistrarse==true){
                            $cordovaToast.show("No se pudo activar las notificaciones, asegurate de tener internet e intentá de nuevo!",'long','center');
                            intentandoRegistrarse=false;
              $scope.notificationEnable=false;
              localStorage.setItem("notificationEnable",$scope.notificationEnable);
                    }
                });

  }

I guess I have to use $scope.$apply() but how knows.




Aucun commentaire:

Enregistrer un commentaire