Skip to content

Forms: cyclic DI dependency when component is both valueAccessor and validator #4253

Closed

Description

@yjbanov

The following pattern results in a cyclic DI dependency (roughly: MyComponent > NgControl > MyComponent):

@Component(
    bindings: const [
      const Binding(NG_VALIDATORS, toAlias: MyComponent, multi: true)
    ])
class MyComponent {
  MyComponent(@Self() NgControl cd) {
    cd.valueAccessor = this;
  }
}

Workaround

@Injectable()
class DeferredValidator {
  Function delegate;

  call(NgControl control) {
    return this.delegate(control);
  }
}

@Component(
    bindings: const [
      DeferredValidator,
      const Binding(NG_VALIDATORS, toAlias: DeferredValidator, multi: true)
    ])
class MyComponent {
  MyComponent(@Self() NgControl cd, DeferredValidator dv) {
    cd.valueAccessor = this;
    dv.delegate = this;
  }
}

Solution

Two possibilities:

  1. We bless the above workaround and perhaps even provide a DeferredValidator as part of ng2 Forms API
  2. We alter the value accessor API so that it does not cause the dependency cycle.

Activity

  1. vsavkin commented on Oct 11, 2015

    @vsavkin
    Contributor

    Fixed via 00a4b2e

  2. angular-automatic-lock-bot commented on Sep 7, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  3. locked and limited conversation to collaborators on Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

effort2: daysfeatureLabel used to distinguish feature request from other issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions