4

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!

1 Answer 1

14

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;
  }
Sign up to request clarification or add additional context in comments.

3 Comments

oh so maybe it's null because i'm using the same component in another point the the code not wrapped by a form ?!
I tried this but the controlContailer returen a null, so i couldn't call valueAccessor of that. How to resolve that?
Thanks - this was exactly the problem that I was having and the @Optional() decorator resolved the issue. For clarity, this was not a problem using the regular ng build command, but it was a problem when running the production build using ng build --prod.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.