8

I have a RadioButtonComponent and a RadioButtonGroupDirective which depend on each other:

RadioButtonComponent:

import { RadioButtonGroupDirective } from "./radio-button-group.directive";
...
constructor(@Optional() @Inject(forwardRef(() => RadioButtonGroupDirective)) radiobuttonGroup: RadioButtonGroupDirective, ...) {

RadioButtonGroupDirective:

import { RadioButtonComponent } from "./radio-button.component";
...
@ContentChildren(forwardRef(() => RadioButtonComponent))
private radioButtons: QueryList<RadioButtonComponent>;

With the latest webpack update in angular-cli, I get the following warning when building:

WARNING in Circular dependency detected:
lib\controls\radio-button\radio-button-group.directive.ts -> lib\controls\radio-button\radio-button.component.ts -> lib\controls\radio-button\radio-button-group.directive.ts

WARNING in Circular dependency detected:
lib\controls\radio-button\radio-button.component.ts -> lib\controls\radio-button\radio-button-group.directive.ts -> lib\controls\radio-button\radio-button.component.ts

In fact, the code works because I use forwardRef() in both cases, specifying that the other class might not be loaded yet. But how can I resolve the warning?

Usually, I would implement an interface for one of the two classes and use this but neither @Inject nor @ContentChildren can work with an interface, right?

2
  • If there is a RadioButtonGroup, I have to delegate certain input events of the RadioButton to it. But does this even make a difference? If I used @Input, I would still need a Reference to the Directive... Commented Aug 23, 2017 at 9:24
  • How are you using these two things in conjunction? Is the directive applied on the button (or group of buttons?) each time the button is used? Or is it not always one to one. Or one to many. Its hard to answer without knowing, but I will say there are almost no instances where using forwardRef to resolve a circular dependency is a good long term solution. Commented Aug 23, 2017 at 10:16

1 Answer 1

1
  1. Arrange your files structure by purpose.

  2. For every folder group, add an index.ts file.

  3. Inside the index.ts, export your module, components, folder, and/or etc.

E.g. Inside component/shared/buttons/index.ts

export * from './radiobuttoncomponent';

outside the radiobutton folder add another index.ts with export * from './radiobutton';

In your import update the reference to use a general path

import { RadioButtonComponent} from "./shared";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.