addListener in AngularJS

7,443 views
Skip to first unread message

Neil Camara

unread,
May 22, 2014, 2:33:31 AM5/22/14
to ang...@googlegroups.com
Hi folks,

What is the addListener equivalent in AngularJS? A plunkr example would be awesome too!

Thanks in advance!

Neil

Michael Dausmann

unread,
May 22, 2014, 5:48:36 AM5/22/14
to ang...@googlegroups.com
hi Neil

You might need to be a bit more specific about what you are trying to achieve.  

In general, AngularJS helps you to avoid adding any listeners to your code directly e.g. instead of adding an on-click event listener directly, you would use an ng-click attribute on the element....

// add event listener to t
var el = document.getElementById("someelement");
el.addEventListener("click", someFunction, false);
<div ng-click="someFunction()">
If you are writing directives and you are familiar with JQuery,  you can add listeners on elements in the link function using a JQuery syntax...

  1. return function(scope, element, attr) {
  2. var startX = 0, startY = 0, x = 0, y = 0;
  3.  
  4. element.on('mousedown', function(event) {
  5. // Prevent default dragging of selected content
  6. event.preventDefault();
  7. startX = event.pageX - x;
  8. startY = event.pageY - y;
  9. $document.on('mousemove', mousemove);
  10. $document.on('mouseup', mouseup);
  11. });


If you want to want your client to listen for events from the server, then you might want to use a socket implementation with an angular wrapper like angular-socket-io (https://github.com/btford/angular-socket-io)

HTH

Michael

Neil Camara

unread,
May 22, 2014, 9:40:11 AM5/22/14
to ang...@googlegroups.com
Hi Michael,

I'm trying to convert the code in line 92. I'd like to know how I should listen for select/clicks on the pie chart. I already have a chart and I'm using google chart directive for angular which someone wrote.


Thanks,

Neil

Neil Camara

unread,
May 22, 2014, 10:07:01 AM5/22/14
to ang...@googlegroups.com
Here is an example google chart for angular

Reply all
Reply to author
Forward
0 new messages