Can we have multiple expression to add multiple ng-class ?
for eg.
<div ng-class="{class1: expressionData1, class2: expressionData2}"></div>
If yes can anyone put up the example to do so.
.
Can we have multiple expression to add multiple ng-class ?
for eg.
<div ng-class="{class1: expressionData1, class2: expressionData2}"></div>
If yes can anyone put up the example to do so.
.
For the ternary operator notation:
An incredibly powerful alternative to other answers here:
Some examples:
1. Simply adds 'class1 class2 class3' to the div:
2. Adds 'odd' or 'even' classes to div, depending on the $index:
3. Dynamically creates a class for each div based on $index
If
$index=5
this will result in:Here's a code sample you can run:
Yes you can have multiple expression to add multiple class in ng-class.
For example:
Below active and activemenu are classes and itemCount and ShowCart is expression/boolean values.
Using a
$scope
method on the controller, you can calculate what classes to output in the view. This is especially handy if you have a complex logic for calculating class names and it will reduce the amount of logic in your view by moving it to the controller:and in the view, simply:
To apply different classes when different expressions evaluate to
true
:To apply multiple classes when an expression holds true:
or quite simply:
Notice the single quotes surrounding css classes.