Is there an easy way to inject an input binding into the deps array of a provider factory? Below obviously does not work.
const myServiceFactory = (object: any) => {
//...
};
@Component({
// ...
inputs: ['object'],
providers: [
{
provide: Object,
useValue: object,
},
{
provide: MyService,
useFactory: myServiceFactory,
deps: [Object]
}
]
})
As a possible solution you can try to do it something like this:
const myServiceFactory = (self: Child) => {
return new MyService(self.param);
};
class MyService {
constructor(private param: string) {}
}
@Component({
selector: 'child',
template: `{{ param }}`,
providers: [
{
provide: MyService,
useFactory: myServiceFactory,
deps: [Child]
}
]
})
export class Child {
@Input() param: any;
constructor(private inj: Injector) { }
ngOnInit() { // or ngOnChanges
let service = this.inj.get(MyService);
}
}