Skip to main content

FormControl | Formulários | Angular

  • FormControl | Formulários | Angular

  • Criando formControl sem o formBuilder

    myControl = new FormControl('default value');
    <input type="text" [formControl]="myControl" />
  • Estados

    • Pristine: true when the user has not changed the element value.
    • Dirty: opposite of pristine — true when the user has changed the element value
    • Valid: true when the element value is valid against all validation rules applied to the element
    • Invalid: opposite of valid — true when the element value is not valid
    • Touched: true when the user has put focus on the element (say clicked on it) and then removed the focus from the element (clicked away from it)
    • Untouched: opposite of touched — means the user hasn’t put focus on the element or hasn’t removed that focus yet
  • Quando queremos que ele valide o estado

    • blur — The FormControl value updates when the HTML element blur event is sent (element loses focus)
    • submit — The FormControl value updates when the HTML element form is submitted.
    • change — The default behavior. User updates are reflected in real-time as the FormControl value.
    myControl = new FormControl('default value', { updateOn: 'blur' });
  • Subscribe

    <p>Value changes: {{ myControl.valueChanges | async }}</p>
    <p>Status changes: {{ myControl.statusChanges | async }}</p>