This is the archived documentation for Angular v5. Please visit angular.io to see documentation for the current version of Angular.

FormControlDirective

npm Package @angular/forms
Module import { FormControlDirective } from '@angular/forms';
Source forms/src/directives/reactive_directives/form_control_directive.ts

Syncs a standalone FormControl instance to a form control element.

In other words, this directive ensures that any values written to the FormControl instance programmatically will be written to the DOM element (model -> view). Conversely, any values written to the DOM element through user input will be reflected in the FormControl instance (view -> model).

Overview

      
      @Directive({ selector: '[formControl]', providers: [formControlBinding], exportAs: 'ngForm' })
class FormControlDirective extends NgControl implements OnChanges {
  viewModel: any
  form: FormControl
  model: any
  update: new EventEmitter()
  set isDisabled: boolean
  ngOnChanges(changes: SimpleChanges): void
  get path: string[]
  get validator: ValidatorFn | null
  get asyncValidator: AsyncValidatorFn | null
  get control: FormControl
  viewToModelUpdate(newValue: any): void
  // inherited from forms/NgControl
  name: string | null
  valueAccessor: ControlValueAccessor | null
  get validator: ValidatorFn | null
  get asyncValidator: AsyncValidatorFn | null
  viewToModelUpdate(newValue: any): void
  // inherited from forms/AbstractControlDirective
  get control: AbstractControl | null
  get value: any
  get valid: boolean | null
  get invalid: boolean | null
  get pending: boolean | null
  get disabled: boolean | null
  get enabled: boolean | null
  get errors: ValidationErrors | null
  get pristine: boolean | null
  get dirty: boolean | null
  get touched: boolean | null
  get status: string | null
  get untouched: boolean | null
  get statusChanges: Observable<any> | null
  get valueChanges: Observable<any> | null
  get path: string[] | null
  reset(value: any = undefined): void
  hasError(errorCode: string, path?: string[]): boolean
  getError(errorCode: string, path?: string[]): any
}
    

How To Use

Use this directive if you'd like to create and manage a FormControl instance directly. Simply create a FormControl, save it to your component class, and pass it into the FormControlDirective.

This directive is designed to be used as a standalone control. Unlike FormControlName, it does not require that your FormControl instance be part of any parent FormGroup, and it won't be registered to any FormGroupDirective that exists above it.

Get the value: the value property is always synced and available on the FormControl instance. See a full list of available properties in AbstractControl.

Set the value: You can pass in an initial value when instantiating the FormControl, or you can set it programmatically later using setValue or patchValue.

Listen to value: If you want to listen to changes in the value of the control, you can subscribe to the valueChanges event. You can also listen to statusChanges to be notified when the validation status is re-calculated.

Example

      
      import {Component} from '@angular/core';
import {FormControl, Validators} from '@angular/forms';

@Component({
  selector: 'example-app',
  template: `
     <input [formControl]="control">
      
     <p>Value: {{ control.value }}</p>
     <p>Validation status: {{ control.status }}</p>
     
     <button (click)="setValue()">Set value</button>
  `,
})
export class SimpleFormControl {
  control: FormControl = new FormControl('value', Validators.minLength(2));

  setValue() { this.control.setValue('new value'); }
}
    

Selectors

      
      [formControl]
    

Inputs

formControl bound to FormControlDirective.form
disabled bound to FormControlDirective.isDisabled

Outputs

ngModelChange bound to FormControlDirective.update

Exported as

Constructor

      
      constructor(validators: Array<Validator | ValidatorFn>, asyncValidators: Array<AsyncValidator | AsyncValidatorFn>, valueAccessors: ControlValueAccessor[])
    

Members

      
      viewModel: any
    

      
      form: FormControl
    

      
      model: any
    

      
      update: new EventEmitter()
    

      
      set isDisabled: boolean
    

      
      ngOnChanges(changes: SimpleChanges): void
    

      
      get path: string[]
    

      
      get validator: ValidatorFn | null
    

      
      get asyncValidator: AsyncValidatorFn | null
    

      
      get control: FormControl
    

      
      viewToModelUpdate(newValue: any): void