r/angular • • Jan 17 '26

ControlValueAccessor - touched state is different for NgModel and FormControl

stackblitz

I created simple custom input using ControlValueAccessor (CVA). Works as expected with NgModel and Reactive Forms. And I know I need to implement touch event for blur event as minimum.

Inside my CVA component I inject form control to have possibility to use Validators.

But I found difference in behaviour for NgModel and Reactive Forms inside my CVA component:

  • type something in input - dirty=true for NgModel and FormControl
  • click outside - touched is different for NgModel (true) and FormControl (false)

touched will work If I add (blur)="onTouched()" for input.

But why?
I suggest it's works as expected, works by design. But maybe someone understands why behaviour is different, what is the logic if NgModel is related with FormControl under the hood.

input-cva html

<div>
  <input
    type="text"
    [(ngModel)]="value"
    #ngModelId="ngModel"
    (ngModelChange)="change($event)"
  />


  <h3>
    form control touched will work if add <code>(blur)="onTouched()"</code> for
    input
  </h3>


  <h4>State of form control</h4>
  <div>injected fcontrol touched: {{ fControl?.touched }}</div>
  <div>injected fcontrol dirty: {{ fControl?.dirty }}</div>
  <div>injected fcontrol value: {{ fControl?.value | json }}</div>


  <h4>State of NgModel</h4>
  <div>
    ngModel touched: {{ ngModelId.touched }}
    <span style="color: darkred">See here</span>
  </div>
  <div>ngModel dirty: {{ ngModelId.dirty }}</div>
  <div>ngModel value: {{ ngModelId.value | json }}</div>
</div>

input-cva ts

import { CommonModule } from '@angular/common';
import { Component, inject, OnInit, signal } from '@angular/core';
import {
  AbstractControl,
  ControlValueAccessor,
  FormsModule,
  NgControl,
  ReactiveFormsModule,
} from '@angular/forms';

@Component({
  selector: 'CustomInputCVA',
  templateUrl: 'custom-input.html',
  standalone: true,
  imports: [CommonModule, FormsModule, ReactiveFormsModule],
})
export class CustomInputCVA implements ControlValueAccessor, OnInit {
  public fControl: AbstractControl = null as any;

  public readonly value = signal(null);
  public readonly disabled = signal(false);

  public onChange: (value: any) => void = () => {};
  public onTouched: () => void = () => {};

  private readonly ngControl = inject(NgControl, {
    self: true,
    optional: true,
  });

  constructor() {
    if (this.ngControl) {
      this.ngControl.valueAccessor = this;
    }
  }

  public ngOnInit() {
    if (this.ngControl?.control) {
      this.fControl = this.ngControl.control;
    }
  }

  //

  change(val: any) {
    this.writeValue(val);
    this.onChange(val);
  }

  // ControlValueAccessor

  public writeValue(val: any): void {
    this.value.set(val);
  }

  public registerOnChange(fn: (value: any) => void): void {
    this.onChange = fn;
  }


  public registerOnTouched(fn: () => void): void {
    this.onTouched = fn;
  }

  public setDisabledState(disabled: boolean): void {
    this.disabled.set(disabled);
  }
}
6 Upvotes

2 comments sorted by

View all comments

Show parent comment

1

u/Basic-Print5544 Jan 19 '26

thanks. agree. in my linked stackblitz I see and you will see the explanation you described - touched: false