question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Cannot instantiate cyclic dependency! NgControl"

See original GitHub issue

After struggling for a couple of days with an error on tests when upgrading our project to Angular 10 (and shallow-render), I’ve found the solution in your playground project on stackblitz.

        .provideMock({
          provide: NG_VALUE_ACCESSOR,
          useClass: DefaultValueAccessor,
          multi: true,
        });

This was what was missing from our tests so we don’t get an error like:

		Error: Template parse errors:
		Cannot instantiate cyclic dependency! NgControl (
		  [ERROR ->]<nz-select #selector
		    (ngModelChange)="onChange($event)"
		    (nzBlur)="onTouched()"
		"): ng:///ShallowTestModule/FormControlImplementationEaseComponent.html@1:2
		
		    at syntaxError (/home/ztp/repos/bee/packages/compiler/src/util.ts:108:17)
		    at TemplateParser.Object.<anonymous>.TemplateParser.parse (/home/ztp/repos/bee/packages/compiler/src/template_parser/template_parser.ts:108:13)

I’ve forked your playground project, and upgraded shallow-render to v10.2.0: https://stackblitz.com/edit/github-mpmyc2?file=examples/test.spec.ts

If I delete that provideMock() code from your example/component-with-forms.spec.ts still works, but not when the ngModel is applied to a component that implements ControlValueAccessor, like on the file “test.spec.ts” on my stackblitz instance.

I’m not understanding this completly so several doubts come across my mind:

  • Is DefaultValueAccessor provided by Angular by default on NG_VALUE_ACCESSOR ?
  • Should this provideMock() code above be added by shallow-render automatically besides neverMocking(NG_VALUE_ACCESSOR).

Not sure even if these doubts make sense… Hope you got a more clear view on this. Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
getsafcommented, Nov 2, 2020

Just published an official release 10.2.1 with this fix!

1reaction
getsafcommented, Oct 28, 2020

I just published a beta release (v10.2.1-0), give this a go and let me know how it goes. If it works out, I’ll make this the next full release.

npm i -D shallow-render@10.2.1-0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Injecting ngControl in custom validator directive, causes cyclic ...
You can inject NgControl via Injector to avoid cyclic dependency. constructor(private _injector: Injector) { } ngOnInit() { console.log(this.
Read more >
Cannot create a custom MatFormFieldControl that implement ...
But it leads to a cyclic dependancy error: ... (in promise): Error: Template parse errors: Cannot instantiate cyclic dependency! NgControl ...
Read more >
NG0200: Circular dependency in DI detected while ... - Angular
Break this loop (or circle) of dependency to resolve this error. This most commonly means removing or refactoring the dependencies to not be...
Read more >
cannot instantiate cyclic dependency angular 9
NgControl. We can get around this problem by getting the NgControl instance directly from the directive Injector. When both the beans depends on...
Read more >
What if I have to get access to the form control of the ... - Medium
Tried to inject private ngControl: NgControl in the constructor with no success -got “Cannot instantiate cyclic dependency! NgControl”.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found