0

I am trying to apply a css class to label if a condition becomes true for making the radio button checked, but its not working.

sample code

<ul style="font-size: 100%;font: inherit;">
        <li  style="margin:10px 0 0 0;" ng-repeat="condition in sampleArray">
             <label class="radio" ng-class="{checked: someCodition.conditionId == condition.conditionId}">  
                      <span class="icon"></span><span class="icon-to-fade"></span>                                  
                        <input type="radio" ng-model="previewContentCondition" ng-value="condition" ng-change="setPreviewContentFromCondition()">
                        <span ng-bind="condition.conditionName"></span>                                                 
            </label>
        </li>                               
</ul>

I tried changing like this but this is also not working.

<ul style="font-size: 100%;font: inherit;">
    <li  style="margin:10px 0 0 0;" ng-repeat="condition in sampleArray">
         <label class="{{someCodition.conditionId == condition.conditionId ? 'radio ' : 'radio checked' }}">    
             <span class="icon"></span><span class="icon-to-fade"></span>                                   
             <input type="radio" ng-model="previewContentCondition" ng-value="condition" ng-change="setPreviewContentFromCondition()">
         <span ng-bind="condition.conditionName"></span>                                                    
         </label>
    </li>                               
</ul>

any solution? if I apply the css directly its working fine.

edit

if I do someCodition.conditionId == 1, class is getting applied to all the radio labels.

7
  • use single brace and ng-class ` ng-class = { condition }` Commented Feb 10, 2017 at 8:55
  • any error in the console ? When you say "not working", you mean the class is not added ? Commented Feb 10, 2017 at 8:57
  • @ValentinCoudert, I am not getting any error, class is not getting applied to the label Commented Feb 10, 2017 at 8:59
  • @gauravbhavsar, I tried your answer but it didn't work Commented Feb 10, 2017 at 9:22
  • What is 'someCodition'? Where is it defined, and is the spelling mistake intentional? Commented Feb 10, 2017 at 9:23

2 Answers 2

1

1) You need to give name to your radio buttons. if you give same name to all radio buttons then only one will be clicked at once.

2) use ng-class instead.

3) Put previewContentCondition in an object $scope.myobj.previewContentCondition. ng-repeat makes its own scope and primitive values will not get in model when radio button clicked.

4) give unique value to radio button so then if it is checked you can get in model. i have used $index that can be get like ng-repeat="condition in sampleArray track by $index"

I made few changes. see working code below:

    <ul style="font-size: 100%;font: inherit;">
       {{myobj.previewContentCondition}}
    <li  style="margin:10px 0 0 0;" ng-repeat="condition in sampleArray track by $index">
         <label ng-class="{'classcolor':$index == myobj.previewContentCondition}">    
             <span class="icon"></span><span class="icon-to-fade"></span>   
             <input type="radio" name="test" ng-model="myobj.previewContentCondition" ng-checked="true" type="radio" value="{{$index}}" >
         <span ng-bind="condition.conditionName"></span>                                                    
         </label>
    </li>                               
</ul>

see working plnkr here.

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

Comments

0

Wrap '' around the class name :

<label class="radio" ng-class="{'checked': someCodition.conditionId == condition.conditionId}"> 

3 Comments

How is your expression someCodition.conditionId == condition.conditionId validated?
This condition is returning value as true I have checked it by printing the condition value.
wrap ' ' around single word class name is optional. When class name is like my-class, then you should wrap it like 'my-class'. Ref: angular ng-class

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.