Angular ERROR in : No provider for NgControl [英] Angular ERROR in : No provider for NgControl
问题描述
我正在创建一个实现 ControlValueAccessor 的组件以在反应式表单中使用,它只是一个输入元素的包装器,上面有一些管道.
我已注入 NgControl 以检索有效/无效状态并将它们传播到内部输入元素.
当在另一个输入中找到输入值时,它是无效的.
在 Stackblitz 上工作正常,但是当我 ng build --prod 时,它会引发错误:
<块引用>错误:无 NgControl 提供程序 ("[ERROR ->])
您是否发现任何问题?
谢谢!
您好,您可以为您的 NgControl 添加@Optional"装饰器.如果找不到提供者,它将使用 null.更多信息请查看:https://angular.io/api/core/Optional >
在您的代码中:
import { Component, Input, OnInit, Self,Optional } from '@angular/core';...构造函数(@Self() @Optional() 私有控制容器:NgControl){this.controlContainer.valueAccessor = this;}
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.
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!
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;
}
这篇关于Angular ERROR in : No provider for NgControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!