2

I am using angular 5 for my project. I am trying to emit event from a child component like

@Output studentUpdated:EventEmitter<any> = new EventEmitter()
updated(e){
   this.studentUpdated.emit(e);
}

and listing in parent component like

<student (studentUpdated)="showSuccess($event)" />

Which work as expected. However in my student component I need to implement several events. e.g. saved, updated, deleted, confirmed, scheduled... etc

I was thinking instead of writing one event per line and calling them individually, is there a possibility that we can initialize array of events and execute them as needed. e.g.

  @Output() events:EventEmitter<any>[] = []
  for(var keyNum in arrayOfEvents)
  {
        var key = arrayOfEvents[keyNum];
        this.events.push(new EventEmitter())
  }

Then on certain event we emit the event based on index.

this.events[0].emit(e)

If we can then how can we do that and also how can we listen to that event on parent component ?

3
  • You're surely not using angular 4, 5 and 6 in the same project. I inferred from your angular5 that you're using 5.x, so I've edited the question to reflect that. It's helpful for now, and for future readers. Please correct that if it's not right. Commented Apr 13, 2018 at 13:24
  • 1
    @msanford thanks for updating the question. I mentioned all of them is because I started with Angular 4 updated to 5 and now moving towards 6. All of these versions are using same EventEmitter class. I assume people are awesome like you and understand what some one asked ;) Commented Apr 13, 2018 at 21:09
  • How kind of you. It's just that the most recent versions, especially 6, do have a lot of changes. Just helping keep things organized. Good luck! Commented Apr 13, 2018 at 21:22

1 Answer 1

5

you can try have another approach to solve your issue, for example, you can restructure your output event data in a way that describe what kind of action your are executing

interface CustomEventData {
 kind : string,
value: any
}

in this way you going to have only eventEmitter output

@Output action:EventEmitter<any> = new EventEmitter()

updated(e){
   this.action.emit({kind "updated", value : e });
}

in your parent component your function that handle the event will react based on the kind of event that is received

showSuccess({ kind , value}) {
    switch(kind)
       case "updated" ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is a good approach, try to have only one EventEmitter and send an object with the type of action you want to perform (update, delete, .....and so on)
In addition, you have the event.type to identify the kind of event triggered
@Ricardo thanks for the answer, it should work as you mentioned. I will change my code.

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.