1

Hello there learning angular and came across the @output decorators in angular. After researching the topic I still have some difficulty wrapping my head around this concept.

Code example:

app.component.html:

<app-tutorials(event)="doSomething($event)"></app-tutorials>

app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  doSomething(event){
    alert("being executed");
  }
}

tutorial.component.html:

<button (click)="beingClicked($event)"_>click me</button>

tutorial.component.ts:

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

@Component({
  selector: 'app-tutorials',
  templateUrl: './tutorials.component.html',
  styleUrls: ['./tutorials.component.css']
})
export class TutorialsComponent{

  @Output() event = new EventEmitter();

  beingClicked(event){
    this.event.emit(event);
  }
}

My current understanding is this, plz correct me if I'm wrong:

  • output is used to transfer data only from child components the parent component.
  • When the button is clicked beingClicked(event) is fired and passes through event info in the argument with $event.
  • We can emit the event with this.event.emit(event); because we first instantiated the event earlier in the class with the statement: @Output() event = new EventEmitter();
  • Now the parent component listens for (event) in the app.component.html:<app-tutorials(event)="doSomething($event)"></app-tutorials>
  • When the event is emitted from the child component the doSomething($event) function is fired and gets passed in the $event

Question:

  1. Is my current understanding correct?
  2. What exactly is this EventEmitter and is it always needed to data upwards?

1 Answer 1

3

Is my current understanding correct?

Your understanding is correct. There are other ways to share data but to notify the parent of an event that needs to be reacted to this is the appropriate way to do it. For more details / examples of component interaction see also the documentation Parent listens for child event.

What exactly is this EventEmitter and is it always needed to data upwards?

The EventEmitter is an event that other code (components, services, etc.) can subscribe to. The owner of the EventEmitter can then fire the event with data whenever it is appropriate and the code that has subscribed to the event can decide how to handle the call or ignore it all together if it is not relevant. See also Event-driven Programming.

It is not necessarily only for upstream (child to parent) it could also be used horizontally (child to child) communication via a shared service dependency or for downstream (also using a shared service dependency) so that child components can decide how to handle an event triggered by a parent component.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.