1

Worked in Angular js small app, using ng-class, dynamically changing the class on checking the check box.

To be more specific, I want to change the background-color of the checkbox, on checking in and off.

Below is my code snippet :

<style>
    .unchecked{
                background-color: red;
                color:yellow;   
            }
            .checked{
                background-color: green;
                color:yellow;
            }
        </style>
        <script type="text/javascript">
        var myController = function($scope){
            console.log("Controller works fine");
            $scope.countries=[
                {'name':'India','capital':'Delhi','ischecked':'false'},
                {'name':'Bangladesh','capital':'Dhaka','ischecked':'false'},
                {'name':'Sri Lanka','capital':'Colombo','ischecked':'false'},
                {'name':'Pakistan','capital':'Islamabad','ischecked':'false'}
            ];
        }
        </script>

<body ng-app="" ng-controller="myController">
        <div ng-repeat="country in countries" class="unchecked" ng-class="{checked:country.ischecked}">
        <input type="checkbox" id="{{country.name}}" name="{{country.name}}" ng-model="country.ischecked">{{country.name}}
        {{country.ischecked}}
    </div>
</body>

1 Answer 1

2

Use ischecked as boolean instead of string.

'ischecked':false

PLUNKR

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

1 Comment

Thanks Mahesh.It helped me a lot, as a beginner.

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.