Santhose Kumar
Santhose Kumar

Reputation: 165

Will AngularJs ng class to dynamically change the css

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

Upvotes: 1

Views: 965

Answers (1)

Donal
Donal

Reputation: 32713

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 }">

Upvotes: 1

Related Questions