Skip to content

Instantly share code, notes, and snippets.

@derlin
Last active May 10, 2016 11:52
Show Gist options
  • Save derlin/9f71a621abfac6fb1b617d676f46bd42 to your computer and use it in GitHub Desktop.
Save derlin/9f71a621abfac6fb1b617d676f46bd42 to your computer and use it in GitHub Desktop.
Angular directives and utils
// some useful directives and snippets for angularJS 1.X applications
(function () {
angular.module('derlin.hover', [])
.directive('hoverClass', hoverClass);
// how to use:
// <ANY hover-class="default-class:hover-class">...
// any of the two can be empty, but the ":" MUST be present.
function hoverClass() {
return {
restrict: 'A',
scope: {
hoverClass: '@'
},
link: function (scope, element) {
var split = scope.hoverClass.split(":");
var clsOut = split[0];
var clsIn = split[1];
element.addClass(clsOut);
element.on('mouseenter', function () {
element.removeClass(clsOut);
element.addClass(clsIn);
});
element.on('mouseleave', function () {
element.removeClass(clsIn);
element.addClass(clsOut);
});
}
};
};
})();
// from http://stackoverflow.com/questions/14544741/how-can-i-make-an-angularjs-directive-to-stoppropagation
.directive('stopEvent', function () {
return {
restrict: 'A',
link: function (scope, element, attr) {
if(attr && attr.stopEvent)
element.bind(attr.stopEvent, function (e) {
e.stopPropagation();
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment