Skip to solution
mediumFrontend

What is the role of NG_VALUE_ACCESSOR in a custom component?

879 views
01

Understand the problem

Explain the NG_VALUE_ACCESSOR provider token.

formsdi
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

NG_VALUE_ACCESSOR is an InjectionToken used to register custom form controls. By providing it with useExisting: forwardRef(() => MyComponent) and multi: true, the control registers into Angular's Forms registry to communicate state with formControlName.

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Registering NG_VALUE_ACCESSOR inside component metadata
import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
  selector: 'app-custom-input',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => CustomInputComponent),
      multi: true
    }
  ],
  template: '<input (input)="onInput($event)" />'
})
export class CustomInputComponent {
  onInput(event: Event) {
    // notify forms engine...
  }
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 27 of 121 decoded in the Angular track. One more won't hurt.

Back to track