• Overview
@angular/forms

NgForm

directive

Creates a top-level FormGroup instance and binds it to a form to track aggregate form value and validation status.

  
    class NgForm extends ControlContainer implements Form ,AfterViewInit {}
  
  

submitted

boolean

Returns whether the form submission has been triggered.

form

The FormGroup instance created for this form.

ngSubmit

Event emitter for the "ngSubmit" event

options

{ updateOn?: FormHooks | undefined; }

Tracks options for the NgForm instance.

updateOn: Sets the default updateOn value for all child NgModels below it unless explicitly set by a child NgModel using ngModelOptions). Defaults to 'change'. Possible values: 'change' | 'blur' | 'submit'.

ngAfterViewInit

void
@returnsvoid

formDirective

The directive instance.

control

The internal FormGroup instance.

path

string[]

Returns an array representing the path to this group. Because this directive always lives at the top level of a form, it is always an empty array.

controls

{ [key: string]: AbstractControl<any, any>; }

Returns a map of the controls in this group.

addControl

void

Method that sets up the control directive in this group, re-calculates its value and validity, and adds the instance to the internal list of directives.

@paramdirNgModel

The NgModel directive instance.

@returnsvoid

getControl

Retrieves the FormControl instance from the provided NgModel directive.

@paramdirNgModel

The NgModel directive instance.

@returnsFormControl<any>

removeControl

void

Removes the NgModel instance from the internal list of directives

@paramdirNgModel

The NgModel directive instance.

@returnsvoid

addFormGroup

void

Adds a new NgModelGroup directive instance to the form.

@paramdirNgModelGroup

The NgModelGroup directive instance.

@returnsvoid

removeFormGroup

void

Removes the NgModelGroup directive instance from the form.

@paramdirNgModelGroup

The NgModelGroup directive instance.

@returnsvoid

getFormGroup

Retrieves the FormGroup for a provided NgModelGroup directive instance

@paramdirNgModelGroup

The NgModelGroup directive instance.

@returnsFormGroup<any>

updateModel

void

Sets the new value for the provided NgControl directive.

@paramdirNgControl

The NgControl directive instance.

@paramvalueany

The new value for the directive's control.

@returnsvoid

setValue

void

Sets the value for this FormGroup.

@paramvalue{ [key: string]: any; }

The new value

@returnsvoid

onSubmit

boolean

Method called when the "submit" event is triggered on the form. Triggers the ngSubmit emitter to emit the "submit" event as its payload.

@param$eventEvent

The "submit" event object

@returnsboolean

onReset

void

Method called when the "reset" event is triggered on the form.

@returnsvoid

resetForm

void

Resets the form to an initial value and resets its submitted status.

@paramvalueany

The new value for the form.

@returnsvoid

name

string | number | null

The name for the control

value

any

Reports the value of the control if it is present, otherwise null.

valid

boolean | null

Reports whether the control is valid. A control is considered valid if no validation errors exist with the current value. If the control is not present, null is returned.

invalid

boolean | null

Reports whether the control is invalid, meaning that an error exists in the input value. If the control is not present, null is returned.

pending

boolean | null

Reports whether a control is pending, meaning that async validation is occurring and errors are not yet available for the input value. If the control is not present, null is returned.

disabled

boolean | null

Reports whether the control is disabled, meaning that the control is disabled in the UI and is exempt from validation checks and excluded from aggregate values of ancestor controls. If the control is not present, null is returned.

enabled

boolean | null

Reports whether the control is enabled, meaning that the control is included in ancestor calculations of validity or value. If the control is not present, null is returned.

errors

Reports the control's validation errors. If the control is not present, null is returned.

pristine

boolean | null

Reports whether the control is pristine, meaning that the user has not yet changed the value in the UI. If the control is not present, null is returned.

dirty

boolean | null

Reports whether the control is dirty, meaning that the user has changed the value in the UI. If the control is not present, null is returned.

touched

boolean | null

Reports whether the control is touched, meaning that the user has triggered a blur event on it. If the control is not present, null is returned.

status

string | null

Reports the validation status of the control. Possible values include: 'VALID', 'INVALID', 'DISABLED', and 'PENDING'. If the control is not present, null is returned.

untouched

boolean | null

Reports whether the control is untouched, meaning that the user has not yet triggered a blur event on it. If the control is not present, null is returned.

statusChanges

Observable<any> | null

Returns a multicasting observable that emits a validation status whenever it is calculated for the control. If the control is not present, null is returned.

valueChanges

Observable<any> | null

Returns a multicasting observable of value changes for the control that emits every time the value of the control changes in the UI or programmatically. If the control is not present, null is returned.

validator

Synchronous validator function composed of all the synchronous validators registered with this directive.

asyncValidator

Asynchronous validator function composed of all the asynchronous validators registered with this directive.

reset

void

Resets the control with the provided value if the control is present.

@paramvalueany
@returnsvoid

hasError

boolean

Reports whether the control with the given path has the error specified.

@paramerrorCodestring

The code of the error to check

@parampathstring | (string | number)[] | undefined

A list of control names that designates how to move from the current control to the control that should be queried for errors.

@returnsboolean
Usage notes

For example, for the following FormGroup:

          
form = new FormGroup({  address: new FormGroup({ street: new FormControl() })});

The path to the 'street' control from the root form would be 'address' -> 'street'.

It can be provided to this method in one of two formats:

  1. An array of string control names, e.g. ['address', 'street']
  2. A period-delimited list of control names in one string, e.g. 'address.street'

If no path is given, this method checks for the error on the current control.

getError

any

Reports error data for the control with the given path.

@paramerrorCodestring

The code of the error to check

@parampathstring | (string | number)[] | undefined

A list of control names that designates how to move from the current control to the control that should be queried for errors.

@returnsany
Usage notes

For example, for the following FormGroup:

          
form = new FormGroup({  address: new FormGroup({ street: new FormControl() })});

The path to the 'street' control from the root form would be 'address' -> 'street'.

It can be provided to this method in one of two formats:

  1. An array of string control names, e.g. ['address', 'street']
  2. A period-delimited list of control names in one string, e.g. 'address.street'
Jump to details