Can you use an angular event listener in a custom directive attribute?


In normal HTML we can directly add listeners to events on HTML elements e.g.

<button onclick="doMyMethod()">Click me</button>

I have a custom directive which emits an event, "dataLoaded". I'd like to declaratively add a handler to the element to deal with that event. However I'm not sure how to do this

I'm making up a methodology here but I'd like to do something like:

<my-element ng-on="'dataLoaded', doMyMethod" ></my-element> <h2>The (undesirable) alternative</h2>

The alternative is to use $scope.$on("dataLoaded", doMyMethod()) in the controller but I would prefer to declare this on the element itself.

Is this possible?


Instead of using an event here, you should pass your method into the directive like so:

angular.module('myApp').directive('myElement', function() { return { scope: { // way to bind method to your directive's scope dataLoaded: '&' }, link: function(scope, element) { ... // instead of triggering data loaded, call method bound to scope scope.dataLoaded(); ... } });

Your HTML:

<my-element data-loaded="doMyMethod"></my-element>


  • Can't get text with text box when it is readonly?
  • Multiple APK Support
  • @MappedSuperclass static weaving with EclipseLink and multiple jars
  • cursive character segmentation in OCR
  • Reason for CSS property precedence?
  • ItemsSource and collections in which only element properties change
  • GWT with a Content Management System
  • regular expression ruby phone number
  • Hierarchical data sorting C#
  • Graceful degradation with Dijit
  • What is going wrong with the training and predictions using TensorFlow?
  • Negative Lookbehind JavaScript
  • Display only 8 lines of text in a container
  • How to implement backward compatible soap webservice (java based)?
  • I need to hash (obfuscate) a column of data in a CSV file. Script preferred
  • Angular2: data binding with routing children
  • how to divide a large Elm program into smaller components
  • In TDD, splitting main class into sub classes during refactoring
  • ngrx - conditionally stop/remove Effect/Action
  • Can't get C++ Boost Pointer Serialization to work
  • What is the dash (“-”) when used with pipe (“|”) in CMD?
  • Can a MEF application built on .NET 4 import types built against .NET 3.5?
  • How to add a “using” statement to the System.Data.Entity namespace [closed]
  • Find symbol dependencies in source code [closed]
  • How to implement arriving behavior with time delta?
  • Extract data between rows r
  • Angular2 - Template reference inside NgSwitch
  • uml Composition relationships to RDF and OWL
  • Set focus to first invalid form element in AngularJS
  • Azure table store snapshot/backup capability
  • xtable package: Skipping some rows in the output
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • NHibernate Validation Localization with S#arp Architecture
  • Change JButton Shape while respecting Look And Feel
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Bug in WPF DataGrid
  • Redux, normalised entities and lodash merge
  • how does django model after text[] in postgresql [duplicate]
  • Linking SubReports Without LinkChild/LinkMaster