Improve this Doc View Source form.FormController
- type in module ng
Overview
FormController keeps track of all its controls and nested forms as well as the state of them, such as being valid/invalid or dirty/pristine.
Each form directive creates an instance of FormController.
Methods
-
$rollbackViewValue();
Rollback all form controls pending updates to the
$modelValue.Updates may be pending by a debounced event or because the input is waiting for a some future event defined in
ng-model-options. This method is typically needed by the reset button of a form that usesng-model-optionsto pend updates. -
$commitViewValue();
Commit all form controls pending updates to the
$modelValue.Updates may be pending by a debounced event or because the input is waiting for a some future event defined in
ng-model-options. This method is rarely needed asNgModelControllerusually handles calling this in response to input events. -
$addControl(control);
Register a control with the form. Input elements using ngModelController do this automatically when they are linked.
Note that the current state of the control will not be reflected on the new parent form. This is not an issue with normal use, as freshly compiled and linked controls are in a
$pristinestate.However, if the method is used programmatically, for example by adding dynamically created controls, or controls that have been previously removed without destroying their corresponding DOM element, it's the developers responsibility to make sure the current state propagates to the parent form.
For example, if an input control is added that is already
$dirtyand has$errorproperties, calling$setDirty()and$validate()afterwards will propagate the state to the parent form.Parameters
Param Type Details control objectcontrol object, either a
form.FormControlleror anngModel.NgModelController -
$getControls();
This method returns a shallow copy of the controls that are currently part of this form. The controls can be instances of
FormController("child-forms") and ofNgModelController. If you need access to the controls of child-forms, you have to call$getControls()recursively on them. This can be used for example to iterate over all controls to validate them.The controls can be accessed normally, but adding to, or removing controls from the array has no effect on the form. Instead, use
$addControl()and$removeControl()for this use-case. Likewise, adding a control to, or removing a control from the form is not reflected in the shallow copy. That means you should get a fresh copy from$getControls()every time you need access to the controls.Returns
Arraythe controls that are currently part of this form
-
$removeControl(control);
Deregister a control from the form.
Input elements using ngModelController do this automatically when they are destroyed.
Note that only the removed control's validation state (
$errorsetc.) will be removed from the form.$dirty,$submittedstates will not be changed, because the expected behavior can be different from case to case. For example, removing the only$dirtycontrol from a form may or may not mean that the form is still$dirty.Parameters
Param Type Details control objectcontrol object, either a
form.FormControlleror anngModel.NgModelController -
$setDirty();
Sets the form to a dirty state.
This method can be called to add the 'ng-dirty' class and set the form to a dirty state (ng-dirty class). This method will also propagate to parent forms.
-
$setPristine();
Sets the form to its pristine state.
This method sets the form's
$pristinestate to true, the$dirtystate to false, removes theng-dirtyclass and adds theng-pristineclass. Additionally, it sets the$submittedstate to false.This method will also propagate to all the controls contained in this form.
Setting a form back to a pristine state is often useful when we want to 'reuse' a form after saving or resetting it.
-
$setUntouched();
Sets the form to its untouched state.
This method can be called to remove the 'ng-touched' class and set the form controls to their untouched state (ng-untouched class).
Setting a form controls back to their untouched state is often useful when setting the form back to its pristine state.
-
$setSubmitted();
Sets the form to its
$submittedstate. This will also set$submittedon all child and parent forms of the form. -
$setValidity(validationErrorKey, isValid, controller);
Change the validity state of the form, and notify the parent form (if any).
Application developers will rarely need to call this method directly. It is used internally, by NgModelController.$setValidity(), to propagate a control's validity state to the parent
FormController.Parameters
Param Type Details validationErrorKey stringName of the validator. The
validationErrorKeywill be assigned to either$error[validationErrorKey]or$pending[validationErrorKey](for unfulfilled$asyncValidators), so that it is available for data-binding. ThevalidationErrorKeyshould be in camelCase and will get converted into dash-case for class name. Example:myErrorwill result inng-valid-my-errorandng-invalid-my-errorclasses and can be bound to as{{ someForm.$error.myError }}.isValid booleanWhether the current state is valid (true), invalid (false), pending (undefined), or skipped (null). Pending is used for unfulfilled
$asyncValidators. Skipped is used by AngularJS when validators do not run because of parse errors and when$asyncValidatorsdo not run because any of the$validatorsfailed.controller NgModelControllerFormControllerThe controller whose validity state is triggering the change.
Properties
-
$pristine
booleanTrue if user has not interacted with the form yet.
-
$dirty
booleanTrue if user has already interacted with the form.
-
$valid
booleanTrue if all of the containing forms and controls are valid.
-
$invalid
booleanTrue if at least one containing control or form is invalid.
-
$submitted
booleanTrue if user has submitted the form even if its invalid.
-
$pending
ObjectAn object hash, containing references to controls or forms with pending validators, where:
- keys are validations tokens (error names).
- values are arrays of controls or forms that have a pending validator for the given error name.
See $error for a list of built-in validation tokens.
-
$error
ObjectAn object hash, containing references to controls or forms with failing validators, where:
- keys are validation tokens (error names),
-
values are arrays of controls or forms that have a failing validator for the given error name.
Built-in validation tokens:
emailmaxmaxlengthminminlengthnumberpatternrequiredurldatedatetimelocaltimeweekmonth
© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
https://code.angularjs.org/1.8.2/docs/api/ng/type/form.FormController