Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  102] [ 3]  / answers: 1 / hits: 62013  / 10 Years ago, mon, june 9, 2014, 12:00:00

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.


More From » angularjs

 Answers
46

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
})

[#70646] Saturday, June 7, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
osvaldo

Total Points: 102
Total Questions: 95
Total Answers: 102

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;