I am creating a form in an Angular 2 application.
Html gives me a send event. In Angular, I could listen to this event using the event binding (submit). In addition, Angular adds an ngSubmit event that I could listen using (ngSubmit).
I understand that submit comes from html and ngSubmit from Angular. I donβt understand why I will need to listen for the ngSubmit special event, and not the normal send event.
I created a plunker that listens for both events, and both seem to do the same.
What is the difference between listening (submit) and (ngSubmit)?
@Component({ selector: 'my-app', template: ` <form (submit)='onSubmit(form)' (ngSubmit)='onNgSubmit(form)' novalidate #form='ngForm'> <input type='text' name='input' [(ngModel)]='input' required> <input type='submit' value='Submit' required> </form> `, }) export class App { input : string; onSubmit(form): void { console.log(`submit: ${this.input}, valid: ${form.valid}`); } onNgSubmit(form): void { console.log(`ng-submit: ${this.input}, valid: ${form.valid}`); } }
angular
Michel vollebregt
source share