Angular ERROR in : No provider for NgControl [英] Angular ERROR in : No provider for NgControl

查看:23
本文介绍了Angular ERROR in : No provider for NgControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个实现 ControlValueAccessor 的组件以在反应式表单中使用,它只是一个输入元素的包装器,上面有一些管道.

我已注入 NgControl 以检索有效/无效状态并将它们传播到内部输入元素.

当在另一个输入中找到输入值时,它是无效的.

这里是 Stackblitz

在 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.

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!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆