1

Got a problem, here the situation:
I want to uncheck the first checkbox when I uncheck the second checkbox.

Let say, by default both checkbox are checked. When I click second checkbox, I want the first checkbox to uncheck also.

HTML:

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox>
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox>


JS:

    $scope.uncheckFirstCheckbox = function(secondCheckbox) {
        if(secondCheckbox.checked === false) {
            $scope.firstCheckbox = false;
        };
    };


The $scope.firstCheckbox turn false but it remain uncheck in HTML.
May I know where could be the problem?

2
  • 1
    try if(!$scope.secondCheckbox) { $scope.firstCheckbox = false; };, dont pass the value which are there on $scope in function calls as a good practise Commented Nov 30, 2016 at 3:45
  • Thank you for your tips :) @entre Commented Nov 30, 2016 at 7:51

1 Answer 1

2

You're trying to access secondCheckbox.checked property but secondCheckbox is already false. You should be only checking secondCheckbox for a false value. Use if(!secondCheckbox) instead for the condition.

Here is a working fiddle of your code.

HTML

<ion-checkbox ng-model="firstCheckbox">First Checkbox</ion-checkbox>
<ion-checkbox ng-model="secondCheckbox" ng-change="uncheckFirstCheckbox(secondCheckbox)">Second Checkbox</ion-checkbox>

JS

$scope.uncheckFirstCheckbox = function(secondCheckbox) {
    if (!secondCheckbox) {
        $scope.firstCheckbox = false;
    };
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! @Divyanshu

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.