【发布时间】:2018-10-08 19:33:34
【问题描述】:
我正在开发一个允许用户动态输入规则的组件,这些规则可以是不同的类型,并且根据规则类型,将捕获不同的值。
我已经创建了动态组件,我的问题是将初始值传递给动态创建的组件。
值因规则而异,因此我尝试在注入器中使用 useValue,但如果我添加参数以接受构造函数中的值,它无法解析参数。
我的组件注入器:
getInjector(rule) {
let inject = this.injectors[rule.name];
if (!inject) {
inject = Injector.create([{ provide: 'initialValues', useValue: rule.values }], this.inj);
this.injectors[rule.name] = inject;
}
return inject;
}
我想要类似的东西:
export class MandatoryRuleComponent implements OnInit {
values: any={};
constructor(initialValues:any) { this.values = initialValues }
ngOnInit() {
if(!this.values.threshold){
this.values.threshold = 9;
}
}
}
完整代码见Stackblitz
【问题讨论】:
标签: angular dependency-injection