[英]Angular2 - Dependency injection not working when injected into a dynamic component of cyclic reference
I have a situation where based on the input selected (radio or checkbox), I add a component dynamically through ajax. 我有一种情况,根据所选的输入(无线电或复选框),我通过ajax动态添加一个组件。
Plunkr: https://plnkr.co/edit/uvGo3RxCkUPeuknVu9m8?p=preview Plunkr: https ://plnkr.co/edit/uvGo3RxCkUPeuknVu9m8 ? p = preview
I explain the flow for better understanding: 我解释了流程以便更好地理解:
First Layer: src/parent/parent.ts
第一层: src/parent/parent.ts
We get the json response from questions.json and populate the questions. 我们从questions.json获取json响应并填充问题。
Based on the controlType
we load either a-component
or b-component
in src/parent/parent.html
. 基于controlType
我们在src/parent/parent.html
加载a-component
或b-component
。
When I select a checkbox in a-component
or a radio button in b-component
, I call createDynamicComponent()
and pass in the componentData
. 当我在a-component
选择一个复选框或在b-component
选择一个单选按钮时,我调用createDynamicComponent()
并传入componentData
。
componentData
is nothing but the metadata information on the next component that needs to be populated, based on the selected option. componentData
只是基于所选选项需要填充的下一个组件的元数据信息。
Now, the componentData
is passed into the dynamic-component
from src/controls/a.html
or src/controls/b.html
现在, componentData
被传递到dynamic-component
从src/controls/a.html
或src/controls/b.html
Inside dynamic-component
, I resolve the provider and inject the questions
data into the entryComponents, ( a-component
and b-component
) 在dynamic-component
内部,我解析提供程序并将questions
数据注入entryComponents( a-component
和b-component
)
Everything was working fine, until I introduced a HService
(which holds the component data building logic) inside both a-component
and b-component
. 一切都运行正常,直到我在a-component
和b-component
引入了HService
(它包含组件数据构建逻辑)。
I am getting the following error, Error: Can't resolve all parameters for BComponent: ([object Object], [object Object], ?)
我收到以下错误, Error: Can't resolve all parameters for BComponent: ([object Object], [object Object], ?)
The last parameter being the HService
inside the constructor of b-component
, If I take out the service and use the commented code, everything works fine. 最后一个参数是b-component
构造函数中的HService
,如果我取出服务并使用注释代码,一切正常。
this.componentData = { 'questions': { "question": "This is a " + this.level + " level question?", "controlType": "radio", "answers": [ "Yes", "No", "None" ] }, "component": BComponent, "level": this.level };
Edit 1: I have the HService
injected into a-component
already and there is no provider resolve error and it is working fine. 编辑1:我已经将HService
注入到a-component
并且没有提供程序解析错误,并且它正常工作。 Only on adding it to b-component
, I face the error. 只有在将其添加到b-component
,我才会面临错误。
Help will be much appreciated!! 帮助将不胜感激!!
I think the problem is based on circular dependencies. 我认为问题是基于循环依赖。
A <=> HService
and B <=> HService
A <=> HService
和B <=> HService
To solve it i would unleash dependencies by creating abstract class for HService
and use it as token: 为了解决这个问题,我将通过为HService
创建抽象类来释放依赖关系,并将其用作标记:
base.h.ts base.h.ts
export abstract class BaseHService {
private componentData: any;
abstract getComponentData(queries: IQ[], level: number): any;
}
app.module.ts app.module.ts
providers: [
{ provide: BaseHService, useClass: HService }, <== this line
{ provide: 'questions', useValue: {} },
{ provide: 'level', useValue: 1 }
],
a.ts a.ts
import { BaseHService } from "../h/base.h";
...
constructor(...private _h: BaseHService) {
b.ts b.ts
import { BaseHService } from "../h/base.h";
...
constructor(...private _h: BaseHService) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.