1

I'm trying to set the status of a button to active based on the value of "OrderBy" with no results, I've tried all the Q/A in stackoverflow.

    <div class="btn-group col-md-offset-5" style="right: 12px !important;">
        <label class="btn btn-default" ng-class="'active':orderProp=='name'">
            <input type="radio" name="color" data-ng-model="orderProp" value="name"> Alphabetical
        </label>
        <label class="btn btn-default" ng-class="'active':orderProp=='price'">
            <input type="radio" name="color" data-ng-model="orderProp" value="price"> Price
        </label>
        <label class="btn btn-default" ng-class="'active':orderProp=='calorie'">
            <input type="radio" name="color" data-ng-model="orderProp" value="calorie"> Calorie
        </label>{{orderProp}}
    </div>

The value of orderProp is changing accordingly but dunno why the active class is not being applied. Thanks

2 Answers 2

2

Try this ng-class = "{'active' : (orderProp=='name')}"

The directive operates in three different ways, depending on which of three types the expression evaluates to:

If the expression evaluates to a string, the string should be one or more space-delimited class names.

If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.

If the expression evaluates to an array, each element of the array should either be a string as in type 1 or an object as in type 2. This means that you can mix strings and objects together in an array to give you more control over what CSS classes appear.

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

3 Comments

Why should anyone try this code? What does this code do? Please add an explanation in the answer.
Thank You..Worked on first hit..can you please explain the difference?
Thanks a lot. That also helped me to solve my problem.
1

ng-class takes a json string as the argument.

Try this - ng-class="{'active':orderProp=='name'}"

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.