Shamoon Shamoon - 1 year ago 49
AngularJS Question

How do I add a class on click to a parent element in AngularJS?

My HTML is as follows:

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

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

Answer Source

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.


<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>


  $scope.activate= function(index){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download