I have a tag:
<body ng-cloak class="foobar" ng-class="{'fixed-header': settings.isFixed}">
In another similar project I have a similar:
<body ng-cloak class="foobar" ng-class="routeClassName">
How can I apply both the routeClassName
and the conditional class?
I have tried
<body ng-cloak class="foobar" ng-class="{'routeClassName','fixed-header': settings.isFixed}">
and similar, but it throws an error. So I guess my syntax is off....
yes , you can do it. Did you try it? What happened? You can use both class and ngClass as the first one gives you the opportunity to apply a class that you want to implement in all cases under any circumstances and the later to apply classes conditionally.
To add a conditional class in Angular we can pass an object to ngClass where key is the class name and value is condition i.e., true or false as shown below. And in the above code, class name will be added only when the condition is true.
javascript - ng-if and ng-class-even/odd doesn't work well together and won't get expected outout - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Here are two ways to accomplish this:
First being, just have Angular interpolate the value since it is just the class name string anyway and then use ngClass
for your conditional:
<body ng-cloak class="foobar {{routeClassName}}" ng-class="{'fixed-header': settings.isFixed}">
Second being, use true in the statement you have tried:
<body ng-cloak class="foobar" ng-class="{'routeClassName': true, 'fixed-header': settings.isFixed}">
I would lean toward the first option since ngClass
is meant for conditional classes based on expressions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With