1

I have a list of checkbox

<input type="checkbox" ng-model="servicesModel" value={{item.name}} id={{item.name}} ng-click="toggleSelection(item.name)"/>

and I want to deselect the selected checkbox

$scope.toggleSelection = function toggleSelection(id) {
window.alert("You can't select this !");
//deselect the selected checkbox
}

Every thing is working fine but I can't found how I could deselect the selected one.

2 Answers 2

1

Pass the model in and assign it false

<input type="checkbox" ng-model="servicesModel" value={{item.name}} id={{item.name}} ng-click="toggleSelection(item.name, servicesModel)"/>

$scope.toggleSelection = function toggleSelection(id, model) {
    window.alert("You can't select this !");
    //deselect the selected checkbox
    model = false; //set
}
Sign up to request clarification or add additional context in comments.

1 Comment

maybe mode.id= false ? but even like this it don't works
0

Update the controller with angular $event as:

  $scope.toggleSelection = function ($event, item) {
      var checkbox = $event.target;
      var action = (checkbox.checked ? 'add' : 'remove');
      $scope.updateToggleSelection(action, item);
  };

  $scope.updateToggleSelection(action, item) {
    if(action =='add') {
       item = true;
     }  

    if(action == 'remove') {
     item = false;
     }
 }

And in the html as:

<input type="checkbox" ng-model="servicesModel" value={{item.name}} id={{item.name}} ng-click="toggleSelection($event, item.name)"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.