懒的翻译了,翻也翻不通顺,直接上原文(其实我就是个搬运工......)

As we can't get NgControl instance directly from DI system since we'll get a circular dependency error. The following diagram shows why it happens if we inject NgControl in our custom value accessor:

如何在自定义的组件(form 控件)中找到FormControl

Now it should be clear that we have NgControl -> FormControlName -> ValueAccessor -> CustomValueAccessor -> NgControl circular dependency

To work around it you can leverageInjector to achieve that:

component.ts

import { NgControl } from '@angular/forms';
export class PasswordComponent implements ControlValueAccessor {
  ...
  ngControl: NgControl;

  constructor(private inj: Injector) {
    ...
  }

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

Plunker Example

相关文章:

  • 2021-12-10
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2021-10-14
  • 2023-04-07
  • 2022-12-23
  • 2022-02-13
猜你喜欢
  • 2022-12-23
  • 2021-10-15
  • 2021-06-15
  • 2023-03-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案