Events
Events is a publish-subscribe style event system for sending and responding to application-level events across your app.
Usage
import { Events } from 'ionic-angular';
constructor(public events: Events) {}
// first page (publish an event when a user is created)
function createUser(user) {
console.log('User created!')
events.publish('user:created', user, Date.now());
}
// second page (listen for the user created event)
events.subscribe('user:created', (user, time) => {
// user and time are the same arguments passed in `events.publish(user, time)`
console.log('Welcome', user, 'at', time);
});
Instance Members
subscribe(topic, handler)
Subscribe to an event topic. Events that get posted to that topic will trigger the provided handler.
Param | Type | Details |
---|---|---|
topic |
string
|
the topic to subscribe to |
handler |
function
|
the event handler |
unsubscribe(topic, handler)
Unsubscribe from the given topic. Your handler will no longer receive events published to this topic.
Param | Type | Details |
---|---|---|
topic |
string
|
the topic to unsubscribe from |
handler |
function
|
the event handler |
Returns:
true if a handler was removed
publish(topic, eventData)
Publish an event to the given topic.
Param | Type | Details |
---|---|---|
topic |
string
|
the topic to publish to |
eventData |
any
|
the data to send as the event |