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';
// first page (publish an event when a user is created)
constructor(public events: Events) {}
createUser(user) {
console.log('User created!')
this.events.publish('user:created', user, Date.now());
}
// second page (listen for the user created event after function is called)
constructor(public events: Events) {
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
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 |
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