Imi Imi - 2 months ago 8
AngularJS Question

How to toggle between diffrent checkboxses Angular

I have four check boxes every click on a check box initiates a different function. One one check box is selected all other should be deselected. I have the following code but the problem is that I cannot toggle between check without first manually removing the check mark and then selecting the other one.
My code:

$scope.checkbox = function(key){
if($scope.checkbox1 === true){
$scope.checkbox2 = false
$scope.checkbox3 = false
$scope.checkbox4 = false
$scope.checkboxValue = 'checkbox one was clicked'
callfunctionOne()
} else if($scope.checkbox2 === true){
$scope.checkbox1 = false
$scope.checkbox3 = false
$scope.checkbox4 = false
$scope.checkboxValue = 'checkbox two was clicked'
callfunctiontwo()
} else if($scope.checkbox3 === true){
$scope.checkbox1 = false
$scope.checkbox2 = false
$scope.checkbox4 = false
$scope.checkboxValue = 'checkbox three was clicked'
callfunctionthree()
} else if($scope.checkbox4 === true){
$scope.checkbox1 = false
$scope.checkbox2 = false
$scope.checkbox3 = false
$scope.checkboxValue = 'checkbox Four was clicked'

callfunctionFour()
}

}


Plunker

Answer
  $scope.checkbox = function(key){
    $scope.checkbox1 = false
    $scope.checkbox2 = false
    $scope.checkbox3 = false
    $scope.checkbox4 = false
    $scope[key] = true;
    $scope.checkboxValue = 'checkbox '+key+' was clicked'
 }

You should consider using radio buttons but this code allows you to do what you want.