Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ERROR in : No provider for NgControl

I'm creating a component implementing ControlValueAccessor to be used in a Reactive Form, it's just a wrapper of an input element with some pipe on it.

I've injected the NgControl in order to retrieve the valid/invalid state and propagate them to the inner input element.

When the input value is found in another input, it is invalid.

Here is the Stackblitz

On Stackblitz is working fine, but when I to an ng build --prod it raises the error:

ERROR in : No provider for NgControl ("[ERROR ->])

Do you see any problems?

Thanks!

like image 785
user2010955 Avatar asked Nov 04 '25 08:11

user2010955


1 Answers

Hello you can add "@Optional" Decorator for your NgControl. It will use null if it cant find a provider. For more information look at: https://angular.io/api/core/Optional

In your Code:

import { Component, Input, OnInit, Self,Optional } from '@angular/core';

...
      constructor(
    @Self() @Optional() private controlContainer: NgControl
  ) {
    this.controlContainer.valueAccessor = this;
  }
like image 91
MullisS Avatar answered Nov 05 '25 22:11

MullisS