Handling Custom jQuery Events in AngularJS
The following example shows a custom directive that detects a custom jQuery events and invokes a function defined by an attribute.
app.directive("onBsShowOffcanvas", function($parse) {
return function linkFn(scope, elem, attrs) {
var fn = $parse(attrs.onBsShowOffCanvas);
elem.on("show.bs.offcanvas", function(e) {
fn.assign(scope, {$event: e});
scope.$apply();
});
}
});
To use the directive in your HTML:
<div on-bs-show-offcanvas="offcanvasHandler($event)">
I recommend that the event object be exposed as $event
since that is customary with AngularJS event directives.
$event
Directives like ngClick
and ngFocus
expose a $event
object within the scope of that expression. The object is an instance of a jQuery Event Object when jQuery is present or a similar jqLite object.
-- AngularJS Developer Guide -- $event