user2010955
user2010955

Reputation: 4011

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!

Upvotes: 4

Views: 7678

Answers (1)

MullisS
MullisS

Reputation: 278

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;
  }

Upvotes: 14

Related Questions