The EventEmitter class is part of the Angular2 core library and is used for communication between components. It allows one component to publish an event, which other components can subscribe to and react accordingly. The syntax for using EventEmitter involves declaring an instance of the class in the component, defining the event as an @Output property, and then emitting the event using the emit() method.
Example 1:
import { Component, EventEmitter, Output } from '@angular/core';
@Component({ selector: 'app-example', template: `
` }) export class ExampleComponent { @Output() buttonClick: EventEmitter = new EventEmitter();
handleClick() { this.buttonClick.emit(); } }
In this example, we define an output event called buttonClick of type EventEmitter. When the button is clicked, the handleClick() method is called and emits the buttonClick event using the emit() method.
Example 2:
import { Component, EventEmitter, Input, Output } from '@angular/core'; import { UserService } from '../services/user.service';
@Component({ selector: 'app-user', template: `
{{ user.name }}
` }) export class UserComponent { @Input() user: any; @Output() deleted: EventEmitter = new EventEmitter();
In this example, we define an output event called deleted of type EventEmitter. When the deleteUser() method is called, it makes a HTTP DELETE request to delete the user from the server. Once the request is successful, it emits the deleted event using the emit() method.
TypeScript EventEmitter.next - 30 examples found. These are the top rated real world TypeScript examples of angular2/core.EventEmitter.next extracted from open source projects. You can rate examples to help us improve the quality of examples.