Submitting a form using the ngSubmit method

To submit our form, we should use an ngSubmit event and attach it to a method in the component. Let us add a method to which we would display the value passed to it in the browser console:

export class RegistrationFormComponent {   onSubmit(formValue) {     console.log(formValue);   } } 

Let us invoke the onSubmit() method whenever an ngSubmit event is triggered:

<form novalidate #formRef="ngForm"                  (ngSubmit)="onSubmit(formRef.value)"> </form> 

Now, once we type some data and click on the Submit button, it will trigger the (ngSubmit) event, which invokes the onSubmit() method on the component. To the onSubmit() method, we are passing our form value using formRef.value and displaying it in the browser ...

Get Building Modern Web Applications using Angular now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.