1

I have the following Angular component:

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

@Component({
  selector: 'app-corkpit',
  templateUrl: './corkpit.component.html',
  styleUrls: ['./corkpit.component.css']
})
export class CorkpitComponent implements OnInit {

  /* 
    @Output is used when you need to emit an event from a child 
    component to the parent (Opp. to what @Input does in the server component !) 
  */
  @Output() serverCreated = new EventEmitter<{ serverName: string , serverContent: string }>();
  @Output() blueprintCreated = new EventEmitter<{ serverName: string , serverContent: string }>();

  newServerName = '';
  newServerContent = '';

  constructor() { }

  ngOnInit() {
  }

  onAddServer() {
    this.serverCreated.emit({ 
      serverName : this.newServerName , 
      serverContent: this.newServerContent 
    })
  }

  onAddBlueprint() {
    this.blueprintCreated.emit({ 
      serverName : this.newServerName , 
      serverContent: this.newServerContent 
    })
  }

}

As you can see I have two function in which I am emitting events, like so:

  onAddServer() {
    this.serverCreated.emit({ 
      serverName : this.newServerName , 
      serverContent: this.newServerContent 
    })
  }

  onAddBlueprint() {
    this.blueprintCreated.emit({ 
      serverName : this.newServerName , 
      serverContent: this.newServerContent 
    })
  }

Now in the template file of the parent component, I have the following markup:

<app-corkpit
  (serverCreated)="onServerAdded($event)" 
  (blueprintCreated)="onBlueprintAdded($event)" 
  ></app-corkpit>

Here you see that on fire of the serverCreated event, the following function is called:

onServerAdded($event)

The function has a variable passed to it $event now if I change this $event to say $data my app breaks, so my question is can this $event variable not be renames to anything that I like? Just like in jQuery if a function accepts an event as an argument, it can be either event or just e or pretty much anything that I like.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • i don't think you can rename $event . As it's not acting like argument name in function. refers these link it may be of some help https://stackoverflow.com/questions/37944199/what-exactly-event-object-do-in-angular-2 and https://angular.io/api/core/EventEmitter – LogicBlower Oct 26 '17 at 17:41
  • Why do you want to change? – Vega Oct 26 '17 at 17:51
  • @LogicBlower Thanks.... Vega , no reason , just was curious . – Alexander Solonik Oct 26 '17 at 18:32

1 Answers1

3

There is no way to rename $event except forking the Angular repository and modifying its code.

There is also no point in renaming because it would only make it harder for others to understand the code without any benefit worth mentioning.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567