@jakubforman/ngx-events
v2.0.0
Published
Use custom event broadcast and listen in anywhere in your Angular app.
Maintainers
Readme
NgxEvents
This library was generated for allow you to use broadcast and listeners anywhere in Angular app.
Install
# For Angular 13
npm i @jakubforman/[email protected]
# For Angular 12
npm i @jakubforman/[email protected]How to use it
Import lib
import {NgxEventsService} from '@jakubforman/ngx-events';Basic example of use
export class AppComponent {
constructor(
private eventsService: NgxEventsService
) {
// Create listener for global use and for remove
const exampleListener = (data: any | { exampleKey: string }) => {
console.log(data.exampleKey)
}
// Get every brodcast onlistener
this.eventsService.on('example', exampleListener)
this.eventsService.on('example', function (data: any | { exampleKey: string }) {
console.log(data.exampleKey)
})
// brodcast in anywhere and send data to every listener with `example`
this.eventsService.broadcast('example', {exampleKey: "exampleValue"}) // send to 2 listeners
this.eventsService.broadcast('user-create', {user: {...}}) // send to 0 listeners
// remove listener
this.eventsService.off('example', exampleListener)
}
}