1

inside view I have

<a ng-class="{'active' : check}" ng-click="doSomething()">1</a>
<a ng-class="{'active' : check}" ng-click="doSomething()">2</a>

 $scope.doSomething= function () {
     $scope.check = true;
 }

I want to set css class to onclicked element only. Using this code I change on multiple elements. How to limit only to one element?

1 Answer 1

2

You can set the value of check variable as follow. And in ng-class compare the value of check to the respective value.

<a ng-class="{'active' : check === 1}" ng-click="check = 1">1</a>
<a ng-class="{'active' : check === 2}" ng-click="check = 2">2</a>

Demo

With a function in controller:

<a ng-class="{'active' : check === 1}" ng-click="doSomething(1)">1</a>
<a ng-class="{'active' : check === 2}" ng-click="doSomething(2)">2</a>

Controller:

$scope.doSomething= function (index) {
    $scope.check = index;
};
Sign up to request clarification or add additional context in comments.

4 Comments

You should use === instead of ==.
it works with your example but I have already wired up event on ng-click. How can I use with my concrete example, is it possible to use two ng-click parameters?
thanks, one more thing. how can I apply this css class on multiple elements using your approach?
Use the same condition on those elements

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.