Emitting an event on ngOnInit()

Viewed 4094

Trying to get an emit an event from child to parent component during initialization. The onchange event is working fine but nothing seems to be firing on init.

The event is from a select menu and we start the select menu off with a default selected value of 2 and we need the parent to know this value:

Child component HTML:

<!-- Show Season Dropdown -->
<div style="float:right">
    <select id="seasonDropDown" aria-label="Select a Season">
                  <option *ngFor="let item of seasons" [value]="item.id" [selected]="item.id === seasonId">Season {{item.id}}</option>
              </select>
</div>

Child component:

 @Output() notify: EventEmitter<number> = new EventEmitter<number>();

  ngOnInit() {
    this.notify.emit(2);
    this.getSeasons();
  }

  public changeSeason(seasonId: number) {
    console.log('change season ' + seasonId)
    this.seasonId = seasonId;
    this.notify.emit(this.seasonId);
  }

We have this component on the parent page:

<app-seasons-dropdown (notify)="onNotify($event)"></app-seasons-dropdown>

We have this listener in the parent component:

  onNotify(event:Event):void {
    console.log('notify');
    const seasonId = (event.target as HTMLSelectElement).value;
    this.getLeaderboardBySeason(Number(seasonId));
  }
0 Answers
Related