1

In this simple example, I am trying to add a class to checkbox input if it is checked. I don't know why this simple example is not working. Can someone help me out?

<html>
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>  
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 
    <input type="checkbox" ng-model="isChecked" ng-class="{peter: isChecked}" />
    <script>
        //Module Declaration
        var app = angular.module('myApp',[]);
        //Controller Declaration
        app.controller('myCtrl',function($scope){
            //No code
        });
    </script> 
</body> 
</html> 
3
  • 1
    Where is the rest of the code? Commented Apr 7, 2016 at 8:20
  • I have added the rest of code. Commented Apr 7, 2016 at 8:25
  • I tried this out in a fiddle and it seems to work correctly to me. Adds the class 'peter' to the checkbox element when it is checked. Fiddle: jsfiddle.net/3symw9f0 Commented Apr 7, 2016 at 8:31

4 Answers 4

3

Your controller wasn't declared correctly.

<script>
var app = angular.module('myApp',[]);
app.controller('myCtrl', function($scope) {
  //No code
});
</script> 
Sign up to request clarification or add additional context in comments.

Comments

3
var app = angular.module('myApp',[]);
app.controller('myCtrl', function($scope) {
  //No code
});

This is right code for controller.

Comments

2

Please provide more code. I'll delete this answer if this is a typo but you are missing /> in the `input field.

Change <input type="checkbox" ng-class="{peter: isChecked}" to <input type="checkbox" ng-class="{peter: isChecked}" />.

Just change your line

app.controller = ('myCtrl',function($scope){
    //No code
});

to

app.controller('myCtrl',function($scope){
    //No code
});

Your controller was declared with the wrong syntax. There is dev console in every browser. You should fire it and first see the errors :-)

Comments

2

Your example works fine. When the checkbox is checked, i can see that:

<input type="checkbox" ng-class="{peter: isChecked}" ng-model="isChecked" class="ng-valid ng-touched ng-dirty ng-valid-parse peter">

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.