Angular

Last Updated: 8/28/2023

ngForm

  • When you apply ngModel directive to an input field, angular creates a FormControl object and associates it with the input field
  • Formcontrol represents an input field
  • Formgroup represents a group of form controls
  • Each form is a form group because it has atleast 1 controls
  • Angular by default applies a directive called ngForm to form tag
  • When you define components or directives you specify selector. When angular sees a selector that matches element it applies directive or component on that element
  • Whenever angular finds a form without the attributes noForm or formGroup, it automatically applies the ngForm directive on that
  • We can create a template variable that reference to the ngForm directive
  • Creates a form group object and associates with the form
  • ngForm exposes output property ngSubmit which is used to handle submit events of form
  • value is the json representation of our form and we can send to API on server for persistence
  • name attribute that we assigned to the input field determines the name of property in the value object
<form #f="ngForm" (ngSubmit)="onSubmit(f)">
</form>

onSubmit(){
	console.log(f.value)
}