Angular

Last Updated: 7/10/2023

Passing event data

  • You can pass value when emitting data
  • The value is available to all subscribers of the event
this.change.emit(this.isSelected);
  • In eventbinding, specify event object
  • $event built-in object in angular
  • In custom components $event represents custom event data
  • In button click $event represents standard DOM event object
(change)="onFavoriteChanged($event)"
  • In the handler add parameter
onFavoriteChanged(isSelected) {
}

Passing event data - complex value

  • You can pass complex data when emitting event
this.change.emit({newValue: this.isSelected});
  • In eventbinding, specify event object
 (change)="onFavoriteChanged($event)"
  • In the handler add parameter
onFavoriteChanged(eventArgs) {
	console.log(eventArgs)
}

Using inline type annotation for event args

onFavoriteChanged(eventArgs: {newValue: boolean}) {
	console.log(eventArgs)
}

Using interface for event args

interface FavoriteChangedEventArgs {
	newValue: boolean
}
onFavoriteChanged(eventArgs: FavoriteChangedEventArgs) {
	console.log(eventArgs)
}