Use Of Event Emitter

Event Emitter

 
Event-emitter is imported from the “@anguar/core” package. It is used in directives and components to emit custom events synchronously or asynchronously and register handlers for those events by subscribing to an instance.
 
So to use this feature we will create 2 components,
  1. UserList Component (Child)
  2. User Component(Parent)
In User Component we will have one textbox and 2 buttons, which are Add and Remove. On click of these two buttons, we will display the username in ListComponent which is a child component.
 
We will integrate List and User component in AppComponent and event will get fired by using event emitter.
 
(AppComponent.Html) In App Component, we are integrating UserList and UserComponent. We will display data into UserList Component page which will be used by template variable "ListValue" which is input for UserList Component page and that will get updated whenever any event will be fired in UserComponent.
  1. <header></header>  
  2.    <div>  
  3.    <hello></hello>  
  4.       <user-list [listValue] = "listData"></user-list>  
  5.       <user-component (userData) = "AddData($event)"></user-component>  
  6. </div>  
(AppComponent.ts)
 
AddData() is an function which will be triggered automatically whenever any event will be performed into UserComponent Page.
  1. import { Component } from '@angular/core';  
  2. @Component({  
  3.    selector: 'my-app',  
  4.    templateUrl: './app.component.html',  
  5.    styleUrls: [ './app.component.css' ]  
  6. })  
  7. export class AppComponent {  
  8.    listData = [];  
  9.    AddData(arr:any[]){  
  10.       this.listData=arr;  
  11.    }  
  12. }  
(UserListCompoenent.Html)
 
UserList Component will display the data. Here ListValue is an input variable which is getting updated on click of Add and Remove button in UserComponent.
  1. <div *ngFor="let data of listValue">  
  2.    <span class="d-block p-2 bg-dark text-white margin10px comment">{{data}}</span>  
  3. </div>  
(UserListComponent.ts)
  1. import { Component,Input } from '@angular/core';  
  2. @Component({  
  3.    selector: 'user-list',  
  4.    templateUrl: './list.component.html',  
  5.    styleUrls: [ '../app/app.component.css' ]  
  6. })  
  7. export class ListComponent {  
  8.    @Input() listValue = [];  
  9. }  
(UserComponent.Html)
  1. <html>  
  2.    <head>  
  3.       </head>  
  4.          <div>  
  5.             <div class="input-group input-group-lg">  
  6.                <input type="text" class="form-control margin10px comment" placeholder="Type UserName..." aria-label="Sizing example input" aria-describedby="inputGroup-sizing-lg" #userName>  
  7.                <button type="button" class="btn btn-success margin10px" (click)="AddMessage(userName)">add</button>  
  8.                <button type="button" class="btn btn-success margin10px" (click)="RemoveMessage(userName)">remove</button>  
  9.             </div>  
  10.       </div>  
  11. </html>  
(UserComponent.ts)
 
UserData name should be same as given in appcomponent .html page. UserData is an output event. Here the event emitter will play their role.
  1. this.userData.next(this.listUser);  
Next function will call AddUser function of appcomponent.ts page with the use of next function of event emitter.
  1. import { Component,Output,EventEmitter } from '@angular/core';  
  2. @Component({  
  3.    selector: 'user-component',  
  4.    templateUrl: './user.component.html',  
  5. })  
  6. export class UserComponent {  
  7.    @Output() userData = new EventEmitter();  
  8.    listUser:any=[];  
  9. AddMessage(userName:HTMLInputElement){  
  10.    this.listUser.push(userName.value);  
  11.    this.userData.next(this.listUser);  
  12. }  
  13. RemoveMessage(userName:HTMLInputElement)  
  14.    {  
  15.       var index = this.listUser.indexOf(userName.value);  
  16.       if (index !== -1) {  
  17.          this.listUser.splice(index, 1);  
  18.       }  
  19.    this.userData.next(this.listUser);  
  20.    }  
  21. }  
Now we will register User and List component into (App.Module.ts) file,
  1. import { NgModule } from '@angular/core';  
  2. import { BrowserModule } from '@angular/platform-browser';  
  3. import { FormsModule } from '@angular/forms';  
  4. import { ListComponent } from '../list/list.component'  
  5. import { UserComponent } from '../user/user.component';  
  6. import { AppComponent } from './app.component';  
  7. import { HelloComponent } from './hello.component';  
  8.   
  9. @NgModule({  
  10.    imports: [ BrowserModule, FormsModule ],  
  11.    declarations: [ AppComponent, HelloComponent ,ListComponent,UserComponent],  
  12.    bootstrap: [ AppComponent ]  
  13. })  
  14. export class AppModule { }  
That's all, the download link is avaliable for a Demo.