I have a reactive form:
<form class="filter-form" [formGroup]="filterForm" (ngSubmit)="applyFilter()">
<mat-form-field (click)="$event.stopPropagation()">
<mat-select formControlName="type" placeholder="Filter Type">
<mat-option *ngFor="let key of filterKeys" [value]="filterValue(key)">{{filterLabel(key)}}</mat-option>
</mat-select>
</mat-form-field>
<mat-form-field (click)="$event.stopPropagation()">
<input matInput formControlName="keyword" placeholder="Keyword" type="text">
</mat-form-field>
<div class="filter-buttons">
<button class="secondary-button" (click)="clearForm()" mat-button>Clear</button>
<button type="submit" mat-button [disabled]="!filterForm.valid">Apply</button>
</div>
</form>
When the user presses enter the form submits but the input values are all null. However, if the user presses the submit button it works as expected.
...
this.filterForm = this.fb.group({
type: [null, Validators.required],
keyword: [null, Validators.required]
});
...
applyFilter() {
const formData = this.filterForm.getRawValue();
console.log(this.filterForm);
this.filterOptions.emit({ id: this.columnId, ...formData });
this.filtered = true;
}
Here's a quick stackblitz. It's a bit dirty in the UI but the reaction is the same. Open your console. If you input values and press enter, form values are null. If you input values and press "apply" the form has values.
keydownevent but does forclickevent. As a starter, try to attach a keydown/keyup event or to manually trigger the CD - monogateuser presses enterwhere? Are you listening to keyboard events? Not seeing any keyUp, keyDown, or keyPress events in your input fields. - Zunayed ShahriarapplyFilterfunction just like pressing the submit button it's just empty. I can add akeydownevent to the form to check for enter and call theapplyFilterfunction but that seems redundant. - dcp3450