Skip to content

Instantly share code, notes, and snippets.

@ferronrsmith
Last active December 17, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferronrsmith/5623471 to your computer and use it in GitHub Desktop.
Save ferronrsmith/5623471 to your computer and use it in GitHub Desktop.
General-purpose Event binding. Bind any event not natively supported by Angular Pass an object with keynames for events to ng-event Allows $event object and $params object to be passed
/**
* General-purpose Event binding. Bind any event not natively supported by
* Angular Pass an object with keynames for events to ng-event Allows $event
* object and $params object to be passed
*
* @example <input ng-event="{ focus : 'counter++', blur : 'someCallback()' }">
* @example <input ng-event="{ myCustomEvent : 'myEventHandler($event,
* $params)'}">
*
* @param ng-event
* {string|object literal} The event to bind to as a string or a hash
* of events with their callbacks
*/
app.directive('ngEvent', [ '$parse', function($parse) {
"use strict";
return function(scope, elm, attrs) {
var events = scope.$eval(attrs.ngEvent);
angular.forEach(events, function(ngEvent, eventName) {
var fn = $parse(ngEvent);
elm.bind(eventName, function(evt) {
var params = Array.prototype.slice.call(arguments);
// Take out first parameter (event object);
params = params.splice(1);
scope.$apply(function() {
fn(scope, {
$event : evt,
$params : params
});
});
});
});
};
} ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment