1
<body ng-app="workApp">
<div ng-controller="firstClaimController">
<input type="checkbox" ng-click="sbcfunction()"> Option 1
<input type="checkbox" ng-click="sbcfunction()"> Option 2
<input type="checkbox" ng-click="sbcfunction()"> Option 3
<input type="checkbox" ng-click="sbcfunction()"> Option 4

            {{myValue}}
        </div>

    <script type="text/javascript" >
        var claimController = angular.module('workApp', []); 
        claimController.controller('firstClaimController', function($scope){
    // Test the controller whether it works
        $scope.myValue = "asdf";
    // 
            $scope.sbcfunction = function () {

            };
    });
    </script>

The above is my code and i got 4 checkboxes. The aim is Option 1 is single select and Options 2-4 are multi-select. If Option 1 is selected, Option 2-4 should be deselected automatically. If Option 2-4 are selected Option 1 should be deselected.

Looking for a better solution with angularjs.

2
  • Hi @Alaksandar! Welcome to Stackoverflow! Does my answer solve your question? If it does you should mark it as accepted, if it doesn't could you please explain me how can I make it better? Thanks! Commented Oct 31, 2014 at 19:47
  • @Josep Thanks for your support. I shall work it out and keep you posted. Commented Nov 13, 2014 at 6:53

1 Answer 1

2

Use ng-change.

Without Functions

This should work in the way that you want it to:

<label><input type="checkbox" ng-model="option1" ng-change="multiOption1=multiOption1&&!option1;multiOption2=multiOption2&&!option1;multiOption3=multiOption3&&!option1"> Option 1</label>
<label><input type="checkbox" ng-model="multiOption1" value="option2" ng-change="option1=!(multiOption1||multiOption2||multiOption3)"> Option 2</label>
<label><input type="checkbox" ng-model="multiOption2" value="option3" ng-change="option1=!(multiOption1||multiOption2||multiOption3)"> Option 3</label>
<label><input type="checkbox" ng-model="multiOption3" value="option4" ng-change="option1=!(multiOption1||multiOption2||multiOption3)"> Option 4</label>

Example

With Functions

If you prefer to use functions, you can do it like this:

In your controller add this:

$scope.option1=true;
$scope.option1Changed=function(){
    $scope.multiOption1=$scope.multiOption1&&!$scope.option1;
    $scope.multiOption2=$scope.multiOption2&&!$scope.option1;
    $scope.multiOption3=$scope.multiOption3&&!$scope.option1;
}
$scope.multiOptionsChanged=function(){
    $scope.option1=!($scope.multiOption1||$scope.multiOption2||$scope.multiOption3);
}

And in your view do this:

<input type="checkbox" ng-model="option1" ng-change="option1Changed()"> Option 1
<input type="checkbox" ng-model="multiOption1" value="option2" ng-change="multiOptionsChanged()"> Option 2
<input type="checkbox" ng-model="multiOption2" value="option3" ng-change="multiOptionsChanged()"> Option 3
<input type="checkbox" ng-model="multiOption3" value="option4" ng-change="multiOptionsChanged()"> Option 4

Example

Sign up to request clarification or add additional context in comments.

2 Comments

Josep, looks like in Your example You can't do multi-select with options 2-4.
oh! sorry give me a sec to fix it! you are right! I thought that you wanted them to be radio buttons! my bad!

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.