Skip to content

Instantly share code, notes, and snippets.

@zgohr
Created January 26, 2013 00:22
Show Gist options
  • Select an option

  • Save zgohr/4639125 to your computer and use it in GitHub Desktop.

Select an option

Save zgohr/4639125 to your computer and use it in GitHub Desktop.
Focus, blur, keyup, keydown, and keypress AngularJS directive
.directive( [ 'focus', 'blur', 'keyup', 'keydown', 'keypress' ].reduce( function ( container, name ) {
var directiveName = 'ng' + name[ 0 ].toUpperCase( ) + name.substr( 1 );
container[ directiveName ] = [ '$parse', function ( $parse ) {
return function ( scope, element, attr ) {
var fn = $parse( attr[ directiveName ] );
element.bind( name, function ( event ) {
scope.$apply( function ( ) {
fn( scope, {
$event : event
} );
} );
} );
};
} ];
return container;
}, { } ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment