Coder Social home page Coder Social logo

angular-event-service's Introduction

๐Ÿ“ก Angular Event Service

An angular service to broadcast and listen global events. It works like Anagular@1 event service.

npm version

Installation

To install this library, run:

$ npm install angular-event-service --save

Using it

Once you have published your library to npm, you can import your library in any Angular application by running:

$ npm install angular-event-service

and then from your Angular AppModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

// Import the module
import { EventsServiceModule } from 'angular-event-service';

@NgModule({
  declarations: [
    AppComponent
    // ...
  ],
  imports: [
    BrowserModule,
    // ...
    // Import forRoot()
    EventsServiceModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once your library is imported, you can use it in your app like this:

import { Component } from '@angular/core';

import { EventsService } from 'angular-event-service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private callbackListener: Function = (payload: any) => {
    console.log(payload);
  };

  constructor(private _eventsService: EventsService) {
  }

  ngOnInit() {
    this._eventsService.on('test', this.callbackListener);
  }

  ngAfterViewInit() {
    this._eventsService.broadcast('test', 'testPayload');
    setTimeout(() => {
      this._eventsService.broadcast('test', 'testPayload2');
    }, 1500);
  }

  ngOnDestroy() {
    this._eventsService.destroyListener('test', this.callbackListener);
  }
}

Make sure to destroy the listener once you destroy the component. If not, you will be registering references to events and calling different references to the same callback each time the event occurs.

Development

To generate all *.js, *.js.map and *.d.ts files:

$ npm run tsc

To lint all *.ts files:

$ npm run lint

License

MIT ยฉ Alex Torres

angular-event-service's People

Contributors

alexrex avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.