The problem is in the translation you mentioned. The original passage says:
ngSubmit Ensures that the form doesn’t Submit when the Handler code throws (which is the default behaviour of Submit) and causes an actual http post request.
The part that matters is "handler code throws"
, this refers to exceptions, example:
if (this.formControls.sellingDate > today) {
throw new Error("Campo data não pode ser maior que hoje.");
}
The translation "é lançado"
really not a good translation for "throws"
, and that’s where it’s causing doubt.
However, I do not advise you to validate forms using exceptions, try using Validators
in your formControls or even use toastr
to notify the user about the problem with the data informed in a more friendly way.
Another point is that to avoid reloads, if that is your problem, you have two options that I consider best:
- pass the
$event
for your method ("handler"
) and call $event.preventDefault();
in the first line; or
- when entering an "if" that is impeditive, use
return false;
right after giving the message to the user.
I hope I’ve helped.
Submit causa postBack, ie reload the page. ngSubmit basically prevents standard Ubmit compartmentalization, having greater control in a SPA application.
– Marconi