Last active
December 17, 2015 14:19
-
-
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
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
/** | |
* 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