mardi 22 mars 2016

Checkboxes somehow auto uncheck in AngularJS [ionicFramework]

I came up with very interesting problem. I'm developing an application with ionic framework. In which I have an array of objects representing my states of the checkboxes and their values. I would like to set up a notification every time a checkbox has been checked and I would like to cancel the given notification when the given checkbox is unchecked. The problem is that every time I check a given checkbox and I try to click something else after that - the current checkbox triggers its ng-click function again without me clicking on it. I tested this in the browser and it works correctly. But it doesn't work with the iOS emulator.

Any thoughts why this is happening?

Note: The code for the notifications is not added during the debugging. Because it won't work in the browser.

I have the following code in my view.

<span ng-repeat="item in alerts">
  <label class="checkbox" for="{{ item.text }}">
    <input type="checkbox" ng-model="item.checked" ng-click="alertSubmit(item)" />
    {{ item.text }}
  </label>
</span>

And this is what I have in my controller.

$scope.alerts = [
{ id: 0, text: "5 seconds", checked: false},
{ id: 1, text: "10 seconds", checked: true},
{ id: 2, text: "15 seconds", checked: false},
{ id: 3, text: "20 seconds", checked: false}
];


$scope.alertSubmit = function(newAlert){
  if(newAlert.checked == true){
    alert("true");
    $scope.alerts[newAlert.id].checked = true;
    var secondsToAdd = 0;
    switch(newAlert.id){
      case 0:
        secondsToAdd = 5;
        break;
      case 1:
        secondsToAdd = 10;
        break;
      case 2:
        secondsToAdd = 15;
        break;
      case 3:
        secondsToAdd = 20;
        break;
    }
    alert($scope.alerts[newAlert.id].checked);
    var alertTime = new Date();
    alertTime.setSeconds(alertTime.getSeconds() + secondsToAdd);
    alert("will schedule a notification");
    // code which shedules notification shuld go here
  }
  else{
    alert("false");
    $scope.alerts[newAlert.id].checked = false;
    alert("will cancel the notification");
    // code which cancel notification shuld go here
  }
}




Aucun commentaire:

Enregistrer un commentaire