Last active
August 29, 2015 14:17
-
-
Save walterg2/dee8846cf38309d8a467 to your computer and use it in GitHub Desktop.
Angular JS Directive for Event Delegation
This file contains 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
; | |
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); | |
}); | |
}); | |
} | |
}; | |
}; |
This file contains 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
<!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