Open In App

AngularJS Forms ngSubmit() Method

In this article, we are going to see what is ngSubmit method in Angular 10 and how to use it.

The ngSubmit() method is called when the ‘submit’ event is triggered on the ngForm.



Syntax:

<form (ngSubmit)='method($event)'></form>

Parameters:



 

Approach: 

Example:




import { Component, Inject } from '@angular/core';
import { FormGroup, FormControl, FormArray, NgForm } from '@angular/forms'
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
    submit(form: NgForm) {
        console.log(form.value);   
    }
}




<form #form="ngForm" (ngSubmit)="submit(form)" novalidate>
    <input name="first" ngModel required #first="ngModel">
    <input name="last" ngModel>
    <button>Submit</button>
</form>

Output:

Reference: https://angular.io/api/forms/NgForm#onsubmit


Article Tags :