liveBook · Manning

ngSubmit

As explained in Pro Angular 16

An ngSubmit binding is Angular template syntax that invokes an expression or component method when a form is submitted.

Bind `(ngSubmit)` on a form to run a component expression when the form is submitted. It handles the form’s submit event, so one binding can handle submission instead of click bindings on individual buttons. It does not, by itself, decide whether the form data is valid. section 13.4.2 section 13.4.3

Submit a product form

Start with `<form (ngSubmit)="addProduct(newProduct)">`. Inside it, bind a named input with `[(ngModel)]="newProduct.name"`, apply `required`, `minlength="5"`, and `pattern="^[A-Za-z ]+$"`, and use a button with `type="submit"`. Submitting calls `addProduct` with the current product. Keep the input inside the form: Angular’s form validation requires a form element. section 13.4.2

Check the whole form before adding the product

Change the opening tag to `<form #form="ngForm" (ngSubmit)="submitForm(form)">`. The `ngForm` reference gives the handler access to whole-form validation. In `submitForm(form: NgForm)`, set `formSubmitted = true`; if `form.valid`, add the product, replace `newProduct` with a fresh `Product`, call `form.resetForm()`, and clear `formSubmitted`. If the form is invalid, keep the data and use `formSubmitted && form.invalid` to show a warning. Field messages can appear after submission even when a field has not been edited. section 13.4.3

Listing 13.20. Enhancing the component in the component.ts file in the src/app folder
import { Component, computed, signal } from "@angular/core";
import { Model } from "./repository.model";
import { Product } from "./product.model";
import { NgModel, ValidationErrors, NgForm } from "@angular/forms";

@Component({
    selector: "app",
    templateUrl: "template.html"
})
export class ProductComponent {
    private model: Model = new Model();

    // ...statements omitted for brevity...

    formSubmitted: boolean = false;

    submitForm(form: NgForm) {
        this.formSubmitted = true;
        if (form.valid) {
            this.addProduct(this.newProduct);
            this.newProduct = new Product();
            form.resetForm();
            this.formSubmitted = false;
        }
    }    
}

The component listing shows the complete `submitForm` method, including its validity check and successful-submit reset. section 13.4.3

How Pro Angular 16 covers it

How deep it goes
Shows a direct handler call, then a form-aware handler with a validity check and reset; does not detail submit-event internals.
Examples
Uses a product-creation form that submits a bound product, then checks form validity and displays errors after submission.
What it assumes
Assumes familiarity with Angular template bindings and component methods; introduces the ngForm reference needed for whole-form validation.
What you can do after
Build it from scratch: bind form submission to a component method that checks validity and resets after success.
How it connects
Connects event binding with ngModel, NgForm, form validation, and validation messages.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.