3

In AngularJS, I'm trying to add/remove a checked class on a parent element, when its child radio button is checked/unchecked.

<label ng-class="{checked: menuType.isChecked0}">
    <input type="radio" name="menuType" ng-model="menuType.isChecked0" />Text 1
</label>
<label ng-class="{checked: menuType.isChecked1}">
    <input type="radio" name="menuType" ng-model="menuType.isChecked1" />Text 2
</label>

Here is a fiddle: http://jsfiddle.net/fAA2w/

There is no controller or any other relative code. If there is a better way to approach this, please share. This seems simple enough, but I cannot find an answer to this question. What am I doing wrong here?

2 Answers 2

15

You need to give the radio input a value.

For more examples see http://docs.angularjs.org/api/ng/input/input%5Bradio%5D.

<div ng-app>
<label ng-class="{checked: isChecked == 1}">
    <input type="radio" name="menuType" ng-model="isChecked" value="1" />Text 1
</label>

<label ng-class="{checked: isChecked == 2}">
    <input type="radio" name="menuType" ng-model="isChecked"  value= "2" />Text 
</label>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, that makes sense. Any other suggestions for accomplishing this?
+1, but only worked when I compared as a string, i.e., ng-class="{checked: isChecked == '1'}"
0

This is the correct way to be doing it. Reason being, when your ng-model changes, your ng-class will pick up on that and update the view, the Angular way!

2 Comments

Well, this code is not working for me. I suppose if this is correct, then something else must be wrong somewhere else. Perhaps this isn't getting processed by Angular at all.
If you can make a demo I'll check it out. Looks right syntax wise tho.

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.