W3cubDocs

/Angular.js 1.4

Improve this Doc View Source form.FormController

  1. type in module ng

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 uses ng-model-options to 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 as NgModelController usually 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 $pristine state.

    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 responsiblity to make sure the current state propagates to the parent form.

    For example, if an input control is added that is already $dirty and has $error properties, calling $setDirty() and $validate() afterwards will propagate the state to the parent form.

    Parameters

    Param Type Details
    control object

    control object, either a form.FormController or an ngModel.NgModelController

  • $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, $submitted states will not be changed, because the expected behavior can be different from case to case. For example, removing the only $dirty control from a form may or may not mean that the form is still $dirty.

    Parameters

    Param Type Details
    control object

    control object, either a form.FormController or an ngModel.NgModelController

  • $setValidity();

    Sets the validity of a form control.

    This method will also propagate to parent forms.

  • $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 can be called to remove the 'ng-dirty' class and set the form to its pristine state (ng-pristine class). 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 submitted state.

Properties

  • $pristine

    boolean

    True if user has not interacted with the form yet.

  • $dirty

    boolean

    True if user has already interacted with the form.

  • $valid

    boolean

    True if all of the containing forms and controls are valid.

  • $invalid

    boolean

    True if at least one containing control or form is invalid.

  • $pending

    boolean

    True if at least one containing control or form is pending.

  • $submitted

    boolean

    True if user has submitted the form even if its invalid.

  • $error

    Object

    Is an 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 given error name.

      Built-in validation tokens:

    • email

    • max
    • maxlength
    • min
    • minlength
    • number
    • pattern
    • required
    • url
    • date
    • datetimelocal
    • time
    • week
    • month

© 2010–2016 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.4.14/docs/api/ng/type/form.FormController