Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a custom event in an AngularJs service

I am working on an AngularJs project. I have a service which sets and removes events on some buttons. This service is utilized by another service which I do not want to interact directly with the buttons. However I would like a button click event to be filtered up through the first service and handled in the second one. Since I don't want the second service to be aware of the buttons, I figure I will need to create a custom event in the first service. How can I create a custom event and fire it when a button is clicked?

Thanks in advance.

like image 552
rmg.n3t Avatar asked Jun 09 '14 21:06

rmg.n3t


People also ask

Which function is used to create custom service in AngularJS?

AngularJs Custom Service using service() : We can create a custom service using service() function like below.

Is used to create custom events in angular?

Custom events are created in Angular using its EventEmitter class. These events are used to communicate to the Parent Component from a child Component.

How do you trigger an event in AngularJS?

element(newNode). triggerHandler('mousedown'); Will trigger the handler. The only thing is, this doesn't include any of the event data, so you will also have to remove the dependency on event.

What is custom service in AngularJS?

In Angular Js, service is basically a single tone objects which used to perform for a specific tasks. Why Need Service. Basically Angular JS services represents the separation of concern using service architectures. Actually AngularJs services consists of JavaScript functions and also used for a particular tasks.


2 Answers

If you want to send an event between services/directives use broadcast:

$rootScope.$broadcast('buttonPressedEvent'); 

And recieve it like this:

$rootScope.$on('buttonPressedEvent', function () {              //do stuff         }) 
like image 135
Chancho Avatar answered Sep 18 '22 22:09

Chancho


Any global scope based integration would hurt namespacing and could result terrible side-effects in middle and large size applications. I recommend a bit more complicated but definitely cleaner solution, using proxy service.

1. Create proxy service

angular.module('app').service('userClickingHelper', [   function() {     var listeners = [];     return {       click: function(args) {         listeners.forEach(function(cb) {           cb(args);         });       },       register: function(callback) {         listeners.push(callback);       }     };   } ]); 

2. Create button directive which is using the userClickingHelper as a dependency.

angular.module('app').directive('onDistributedClick', ['userClickingHelper', function (userClickingHelper) {     return {         restrict: 'A',         link: function (scope, element) {             element.on('click', userClickingHelper.click);         }     }; }]); 

3. Register your unrelated service with using the proxy.

angular.module('app').service('myUnrelatedService', [function (userClickingHelper) {     userClickingHelper.register(function(){         console.log("The button is clicked somewhere");     }); }]); 

The result is an isolated scope of event distribution, yet neither the directive, nor the service knows about each other. It is also easier to unit-test.

I am using similar solution for globally accessable "Loading" modal so I can abstract the way a controller/service is saying "user should not click or anything right now".

like image 43
Mano Kovacs Avatar answered Sep 22 '22 22:09

Mano Kovacs