Skip to content

Instantly share code, notes, and snippets.

@bbrown
Created June 30, 2015 19:13
Show Gist options
  • Select an option

  • Save bbrown/3791ca9d032b437cfd12 to your computer and use it in GitHub Desktop.

Select an option

Save bbrown/3791ca9d032b437cfd12 to your computer and use it in GitHub Desktop.
Angular directive that toggles a class on an element and insures that no other elements have that class
angular.module("hedge").directive("toggleClassSingularly", function()
{
return {
restrict: "A",
link: function(scope, element, attrs)
{
scope.$root.$on("singular-class-toggled", function(event, args)
{
if (args.class === attrs.toggleClassSingularly && args.element !== element)
{
element.removeClass(args.class);
}
});
element.bind("click", function()
{
element.toggleClass(attrs.toggleClassSingularly);
scope.$root.$broadcast("singular-class-toggled", { class:attrs.toggleClassSingularly, element:element });
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment