Reputation: 3862
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.
Upvotes: 1
Views: 2376
Reputation: 4401
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)"/>
Upvotes: 0
Reputation: 104795
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
}
Upvotes: 1