angular-coms

A simple communication service to allow easy passing of events around an angular application
Homepage:
3 people use it
Submitted by: 294948?v=3 IamAdamJowett

angular-coms

A communication service to allow easy passing and listening of events (with optionaldata) around an angular application. Events are broadcast from the $rootScope, making it easy to listen to events from anywhere in your application.

Installation

There are two easy ways to install the checkie directive:

Bower

To install via Bower, run:

Manual download

Download the coms.service.js file, and include it in your index.html file with something like:

Also be sure to include the module in your app.js file with:

Usage

Sending signals (events)

Sending events are as easy as calling the sendSignal method. You pass the event string to be sent as the first parameter, and any data you wish to pass with the event as the second.

Sometimes with the async nature of AngularJS, you need to delay the sending of a call until something else is ready. This can be done as follows, in this case, delaying the sending of the event by 400ms:

Listening for signals (events)

To listen for an event, you can use the onSignal method, which takes a signal string as the first parameter, a function to listen to the event and a scope to automatically remove the listener if the scope is destroyed (handy for controlling memory leaks).

Deregistering event listeners

Like native angular events, the onSignal method returns the de-registering function as part of the call. You can assign this to a variable and call it later to manually remove a listener. Listeners are de-registed automatically when the scope is destroyed as long as you pass the scope as the last parameter of the listener.

comments powered by Disqus
This page was last updated over 2 years ago.