Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkh44/808cc4e16ef61bb24374 to your computer and use it in GitHub Desktop.
Save tkh44/808cc4e16ef61bb24374 to your computer and use it in GitHub Desktop.
Simple directive to toggle class on click. Can either be applied to element defined on or a child of $element.
app.directive('toggleClass', function () {
return {
restrict: 'A',
scope: {
target: '@',
toggleClass: '@'
},
link: function ($scope, $element, $attr) {
$scope.$targetElement = angular.isDefined($scope.target)? $element.find($scope.target): $element;
$element.bind('click', function (e) {
$scope.$targetElement.toggleClass($scope.toggleClass);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment