Skip to content

Instantly share code, notes, and snippets.

@walterg2
Last active August 29, 2015 14:17
Show Gist options
  • Save walterg2/dee8846cf38309d8a467 to your computer and use it in GitHub Desktop.
Save walterg2/dee8846cf38309d8a467 to your computer and use it in GitHub Desktop.
Angular JS Directive for Event Delegation
;
var Northwoods = Northwoods || {};
Northwoods.Directives = Northwoods.Directives || {};
Northwoods.Directives.FocusableForm = function ($animate, $timeout) {
return {
restrict: 'A',
link: function (scope, element) {
var FOCUSED_CLASS = 'focused',
elements = element.find('input');
elements.push(element.find('button'));
angular.forEach(elements, function (value) {
angular.element(value).on('focus', function () {
$timeout(function () {
$animate.setClass(element, FOCUSED_CLASS, '');
}, 0);
}).on('blur', function () {
$timeout(function () {
$animate.setClass(element, '', FOCUSED_CLASS);
}, 0);
});
});
}
};
};
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
</head>
<body>
<form data-nw-focusable-form>
<label for="myField">Label</label>
<input type="text" name="myField" id="myField"/>
<button>Save</button>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment