DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Code Play #1: WebSockets With Vert.x and Angular
  • Angular vs. React: Which To Choose for Front-End in 2024
  • Virtual Network Functions in VPC and Integration With Event Notifications in IBM Cloud
  • WebSocket vs. Server-Sent Events: Choosing the Best Real-Time Communication Protocol

Trending

  • Understanding Escape Analysis in Go
  • Real-Time Communication Protocols: A Developer's Guide With JavaScript
  • 6 Agile Games to Enhance Team Building and Creativity
  • DZone's Cloud Native Research: Join Us for Our Survey (and $750 Raffle)!
  1. DZone
  2. Coding
  3. Frameworks
  4. Event Emitter Example with Angular 7

Event Emitter Example with Angular 7

Take a look at this brief example of how to construct an event emitter using Angular 7.

By 
ARINDAM GOSWAMI user avatar
ARINDAM GOSWAMI
·
Jun. 01, 20 · Tutorial
Like (3)
Save
Tweet
Share
9.6K Views

Join the DZone community and get the full member experience.

Join For Free

Here I am going to show you a basic example of event emitter with Angular 7. Here it goes.

The app component is the parent component. It has two child components. 

  1. my-comp -> used to insert the data into an array using event emitter
  2. my-comp2  --> used to fetch the data in the array using event emitter decorator.

my-comp.component.html (Contains the input Forms and submit button)

========================

HTML
 




x
13


 
1
<form>
2

          
3
    <label style="margin-top: 30px;" class="label">Enter user Name</label>
4

          
5
    <input  type="text" [(ngModel)] ="username" class="form-control" style="margin-top: 30px;" name="username"/>
6

          
7
    <label style="margin-top: 30px;" class="label">Enter city</label>
8

          
9
    <input  type="text" [(ngModel)] ="city" class="form-control" style="margin-top: 30px;" name="city"/>
10

          
11
    <button type="submit" (click) = "OnSubmitFun()" class="btn btn-primary btn-block btn-lg" style="margin-top: 30px;" >Press Me Plz</button>
12

          
13
</form>


  ========================

my-comp.component.ts 

See the Event emitter is created here. 

JavaScript
 




xxxxxxxxxx
1
33


 
1
import { Component, OnInit, Output, EventEmitter,Input } from '@angular/core';
2

          
3
@Component({
4
  selector: 'app-my-comp',
5
  templateUrl: './my-comp.component.html',
6
  styleUrls: ['./my-comp.component.css']
7
})
8
export class MyCompComponent implements OnInit {
9

          
10
username : string;
11
city : string;
12

          
13
@Output() userArrayOut = new EventEmitter();
14

          
15
OnSubmitFun () {
16

          
17
  const user = {
18
      U : this.username,
19
      C : this.city
20
  }
21
  
22
  if( this.username != '')  {
23
          this.userArrayOut.emit(user);
24
          this.username = '';
25
          this.city ='';
26
  }
27
} 
28
ngOnInit(): void {
29
}
30

          
31

          
32
}
33

          



In the app-component.ts (Mother Component)

JavaScript
 




xxxxxxxxxx
1


 
1
userArray = [];
2

          
3
  storeUser (userArrayOut) {
4

          
5
    this.userArray.push(userArrayOut);
6

          
7
    console.log(this.userArray);
8

          
9
 }



In the app-component.html

HTML
 




xxxxxxxxxx
1


 
1
<app-my-comp (userArrayOut)="storeUser($event)"></app-my-comp>
2
<app-my-comp2 [users] = "userArray"></app-my-comp2> 



Please make sure the event name userArrayOut exactly matches with the below. 

 @Output() userArrayOut = new EventEmitter();  

Here in child component (my-comp), the data is populated in the userArrayOut array.

Then in mother app-component.ts, it is stored in the userArray variable. After that in the app-component.html file,  it is again stored in the users array. See the 2nd line in app-componnet.html.

Now the data insertion into the array is completed. Here goes the way to show the data in the other component.

my-comp2.component.ts

=====

JavaScript
 




x


 
1
export class MyComp2Component implements OnInit {
2

          
3
  constructor() { }
4

          
5
  ngOnInit(): void {
6
  }
7

          
8
  @Input() users : {}
9

          
10
}


Just add the @Input decorator users array. Now the user array is filled with data.

Now, print the data in the my-comp2 component by iterating users array.

my-comp2.component.html

=====

HTML
 




x





1
<ul>
2
    <li *ngFor="let user of users">
3
      Username : - {{user.U }}
4
      city :- {{user.C}}
5
    </li>
6
  </ul>



That is it. Once you fill the information and click on the submit button, the array will be loaded and printed as below.

Array output

Event AngularJS

Opinions expressed by DZone contributors are their own.

Related

  • Code Play #1: WebSockets With Vert.x and Angular
  • Angular vs. React: Which To Choose for Front-End in 2024
  • Virtual Network Functions in VPC and Integration With Event Notifications in IBM Cloud
  • WebSocket vs. Server-Sent Events: Choosing the Best Real-Time Communication Protocol

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: