AngularJS toggle class using ng-class


AngularJS toggle class using ng-class

I am trying to toggle the class of an element using ng-class

$scope.isAutoScroll = function()
$scope.autoScroll = ($scope.autoScroll) ? false : true;
return $scope.autoScroll;

Basically, if $scope.autoScroll is true, I want ng-class to be icon-autoscroll and if its false, I want it to be icon-autoscroll-disabled
What I have now isn’t working and is producing this error in the console
Error: Lexer Error: Unexpected next character at columns 18-18 [?] in expression [{(isAutoScroll()) ? ‘icon-autoscroll’ : ‘icon-autoscroll-disabled’}].
How do I correctly do this?
solution 1: (outdated)

solution 2:
I wanted to update the solution as Solution 3, provided by Stewie, should be the one used. It is the most standard when it comes to ternary operator (and to me easiest to read). The solution would be

translates to:
if (autoScroll == true) ? //use class ‘icon-autoscroll’ : //else use ‘icon-autoscroll-disabled’


Solution 1:

How to use conditional in ng-class:

Solution 1:

<i ng-class="{'icon-autoscroll': autoScroll, 'icon-autoscroll-disabled': !autoScroll}"></i>

Solution 2:

<i ng-class="{true: 'icon-autoscroll', false: 'icon-autoscroll-disabled'}[autoScroll]"></i>

Solution 3 (angular v.1.1.4+ introduced support for ternary operator):

<i ng-class="autoScroll ? 'icon-autoscroll' : 'icon-autoscroll-disabled'"></i>


Solution 2:

As alternate solution, based on javascript logic operator ‘&&’ which returns the last evaluation, you can also do this like so:

<i ng-class="autoScroll && 'icon-autoscroll' || !autoScroll && 'icon-autoscroll-disabled'"></i>

It’s only slightly shorter syntax, but for me easier to read.

Solution 3:

Add more than one class based on the condition:

<div ng-click="AbrirPopUp(s)" 
ng-class="{'class1 class2 class3':!isNew, 
           'class1 class4': isNew}">{{ isNew }}</div>

Apply: class1 + class2 + class3 when isNew=false,

Apply: class1+ class4 when isNew=true

Solution 4:

<div data-ng-init="featureClass=false" 
     data-ng-class="{'active': featureClass}">
    Click me to toggle my class!

Analogous to jQuery’s toggleClass method, this is a way to toggle the active class on/off when the element is clicked.

Solution 5:

autoscroll will be defined and modified in the controller.

<span ng-class= "autoscroll?'class_if_true':'class_if_false'"></span>

Add multiple classes based on condition by:

<span ng-class= "autoscroll?'first second third':'classes_if_false'"></span>

Solution 6:

I made this work in this way:

<button class="btn" ng-click='toggleClass($event)'>button one</button>
<button class="btn" ng-click='toggleClass($event)' >button two</button>

// in your controller

 $scope.toggleClass = function (event){