0

How can I have one checkbox checked by default when the page loads between two toggling check boxes. my code:

jsfiddle: http://jsfiddle.net/Lvc0u55v/456/

function MyCtrl($scope) {
    $scope.changeAxis1 = function() {
        if ($scope.thirtyDay) {
                    $scope.wholeTimeline = false;

    }
                };

     $scope.changeAxis2 = function() {
            if($scope.wholeTimeline) {
                    $scope.thirtyDay = false;  

                }
    };
};

view

 <label class='checkboxes' id='checkbox_1'>
        <input type="checkbox" ng-model="thirtyDay" ng-change="changeAxis1()">
        Two
        </label>
         <label class='checkboxes' id='checkbox_2'>
        <input type="checkbox" ng-model="wholeTimeline" ng-change="changeAxis2()">
        One
        </label>
3
  • 1
    Set model = true for ex. $scope.thirtyDay = true; Commented Mar 1, 2016 at 10:31
  • yes I have already tried that it did not work Commented Mar 1, 2016 at 10:33
  • 1
    Fiddle here Commented Mar 1, 2016 at 10:37

2 Answers 2

2

Initialize one of the values to true in your controller:

function MyCtrl($scope) {
    $scope.changeAxis1 = function() {
        if ($scope.thirtyDay) {
            $scope.wholeTimeline = false;
        }
    };

    $scope.changeAxis2 = function() {
         if($scope.wholeTimeline) {
            $scope.thirtyDay = false;  
         }
    };

    $scope.thirtyDay = true;
};

You also need to have ng-app and ng-controller in your view in order for Angular to do anything:

<div class="checkbox" ng-app="" ng-controller="MyCtrl">

http://jsfiddle.net/Lvc0u55v/457/

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

Comments

1

You can use ng-checked="expression"

<input type="checkbox" ng-checked="expression" ng-model="thirtyDay" ng-change="changeAxis1()">
        Two

and put some logic for expression how you want the checkbox to be checked

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.