5

My HTML is as follows:

<div class="cell">
  <div class="offset-container pull-left">
    <i data-ng-click="doCtrlStuff()"></i>
  </div>
</div>

When you click the <i>, I want to add an active class to the parent that has .cell currently. How is this doable with AngularJS?

1
  • Just a quick word of advice: next time don't ask 1 question and then ask yet another in some comments, especially when you seem to already know you had 2 questions. Put them all in the original post so that people can help you easier. Commented Oct 5, 2013 at 15:41

4 Answers 4

6

OK, according to your last comment, if your cells are in a loop you should have mentioned that in your question. I'm gonna assume you use ng-repeat.

I have something like this which works. The active class also get removed if you click another.

HTML:

<div ng-repeat="cell in [0,1,2]" data-ng-class="{cell:true, active:index=='{{$index}}'}">
    <div class="offset-container pull-left">
        <i data-ng-click="activate($index)">Activate Me</i>
    </div>
</div>

Controller:

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

3 Comments

@NicolasMose is there a way to remove active class if clicked the same button again (toggle class - add and remove)?
@NicolasMose I did the changes to your code as I requested to you above. I got it answered here: stackoverflow.com/questions/38620253/…
This works correctly, just that instead of activate('{{$index}}') it would be activate($index)
2

Here is one way, using ng-class

<div class="cell" ng-class="{'active': isActive==true}">
  <div class="offset-container pull-left">
    <i data-ng-click="doCtrlStuff()">clicky</i>     
  </div>
</div>

controller:

function MyCtrl($scope) {
    $scope.doCtrlStuff = function(){
        $scope.isActive = true;
    }             
}

2 Comments

How can I set all of the other cell's to not be active when one is clicked?
Depends on how your code is setup. Can you add to your sample code so that we can see exactly what you are trying to accomplish?
0
<div class="cell" ng-class="{'active': isActive}">
  <div class="offset-container pull-left">
    <i data-ng-click="isActive = !isActive"></i>
  </div>
</div>

Comments

0

You can do this from here: http://docs.angularjs.org/api/ng.directive:ngClass

<div data-ng-class="{cell:true, active:clickedIcon}">
  <div class="offset-container pull-left">
    <i data-ng-click="doCtrlStuff()"></i>
  </div>
</div>

You would use a class:boolean pattern for the ng-class expression.

And in your controller:

$scope.doCtrlStuff = function() {
    $scope.clickedIcon = true;
}

UPDATE:

If you want to do a radio button:

<div data-ng-class="{cell:true, active:clickedDogIcon}">
  <div class="offset-container pull-left">
    <i data-ng-click="doDogStuff()"></i>
  </div>
</div>
<div data-ng-class="{cell:true, active:clickedCatIcon}">
  <div class="offset-container pull-left">
    <i data-ng-click="doCatStuff()"></i>
  </div>
</div>

$scope.doDogStuff = function() {
    $scope.clickedDogIcon = true;
    $scope.clickedCatIcon = false;
}
$scope.doCatStuff = function() {
    $scope.clickedDogIcon = false;
    $scope.clickedCatIcon = true;
}

5 Comments

How can I remove the active class from all other cells?
I would have a different variable for each parent. So you can do clickedIcon1, clickedIcon2, etc... and set the others to false so that ng-class will remove that class when they are false.
The cells are in a loop. So I can't do the 1, 2, 3, etc approach
If it's in a loop, then you might just want to do jqlite (which is part of Angular) or jquery selectors using addClass & removeClass.
Yes this last part is really bad practice and could get very messy very quickly. What if you have 10 different cells. Yo would need 10 different functions each changing 10 variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.