Created
February 26, 2015 16:29
-
-
Save bubbleheadinc/a05ad99c2e2a102e7adc to your computer and use it in GitHub Desktop.
Angular - click child, set class on ng-repeat parent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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