1

How to apply the css class dynamically in ng-class based on condition.

Code: <input type="text" id="ProcessRight_SearchUserLeft" class="inputSearch" placeholder="Search" ng-model="selectedUserOne" ng-class=""/>

Question: if i have condition bool selecteduser if it is true red want to apply otherwise green want to apply

6
  • you want to apply css dynamically? is it? Commented May 27, 2015 at 13:20
  • You need to be more specific with what you want help with. Please explain further. Commented May 27, 2015 at 13:20
  • yes now i have edited. Commented May 27, 2015 at 13:22
  • 1
    What condition ? Which class ? Commented May 27, 2015 at 13:24
  • if i have condition bool selecteduser if it is true red want to apply otherwise green want to apply. Commented May 27, 2015 at 13:26

1 Answer 1

1

If you are asking if you can change the css class dynamically with ng-class. The answer is yes.

For example, if in your view you could set a field in your model to true and then dynamically set the class attribute using ng-class based on the value of that field.

<!-- toggle a variable to true or false -->
<input type="checkbox" ng-model="someclass"> Use Some Class?
<input type="checkbox" ng-model="someotherclass"> Use Some Other Class?

<!-- add the class 'some-class' if the variable 'someclass' is true -->
<div ng-class="{ 'some-class': someclass, 'some-other-class': someotherclass }">
Sign up to request clarification or add additional context in comments.

1 Comment

@SanthoseKumar glad to be of help.

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.