Angular 5 ngSubmit @Output fires twice

Issue

CUSTOM COMPONENT

// ...

@Output() submit: EventEmitter < any > = new EventEmitter();

// ...

onFilterSubmit($event): void {
  this.submit.emit($event);
  this.formData = {
    minDate: new Date().toISOString(),
    maxDate: new Date().toISOString()
  };
}
<form (ngSubmit)="onFilterSubmit(formData)">

    <!-- -- -->
    <button mat-button
        mat-raised-button
        [disabled]="reqsForm.form.invalid" 
        type="submit"
        color="primary">
        {{labels.submit}}
    </button>
</form>

OUTER COMPONENT

// ...

onFilterSubmit($event): void {
  console.info("FORM SUBMIT", $event);
}
<custom-component (submit)="onFilterSubmit($event)">

  <!-- -- -->

</custom-component>

OUTPUT

FORM SUBMIT > Object

FORM SUBMIT > Object

Solution

The reason why this was happening is that an event called “submit”
is catchable already from outside the custom component.

I solved by changing the custom event name to filterSubmit

Note also that the type submit on the button – in this use case – is virtually useless, since as default one button in a form will be of type submit.

Answered By – Sampgun

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.