Skip to content

Instantly share code, notes, and snippets.

@bubbleheadinc
Created February 26, 2015 16:29
Show Gist options
  • Save bubbleheadinc/a05ad99c2e2a102e7adc to your computer and use it in GitHub Desktop.
Save bubbleheadinc/a05ad99c2e2a102e7adc to your computer and use it in GitHub Desktop.
Angular - click child, set class on ng-repeat parent
// Angular ng-repeat and setting class on item from item's child
// Clicking child sends it's parents index to the controller
// Controller toggles the toggleState boolean and the index lets us
// only add classes to the parent of the clicked child
// in controller
$scope.toggleParentClass = function($index){
$scope.parentIndex = $index;
$scope.toggleState = !$scope.toggleState;
};
// in view
<ul>
<li ng-repeat="item in items" ng-class="{'is-active' : $index==parentIndex && toggleState}">
<div on-click="toggleParentClass($index)">Click Here</div>
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment