NG_VALUE_ACCESSOR 和注入的 NgControl 如何相互替代?

问题描述

我在我的组件中经常使用 ControlValueAccessor 接口。要使其工作,您需要将 NG_VALUE_ACCESSOR 传递给组件提供程序,其中 useExisting 字段中包含当前组件类的值。最近发现还有一种方法,可以注入NgControl,将valueAccessor中的ngOnInit字段设置为组件类实例的值。像这样:

class MyControlComponent {
  
  private ngControl: NgControl;
  
  constructor(
    private inj: Injector,) {}

  ngOnInit() {
    this.ngControl = this.inj.get(NgControl);
    this.ngControl.valueAccessor = this;
  }
}

我只是想知道,当您提供 NG_VALUE_ACCESSOR 令牌和 useExisting 时,这到底发生了什么?有人可以详细说明此提供程序的工作方式吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)