I want to dynamically configure the injector when ngOnInit
is triggered.
Here's my approach:
<my-component [config]="someConfig"></my-component>
// my.component.ts
import { CONFIG_TOKEN } from 'injector_tokens';
@Component({
selector: 'my-component',
template: '<p>need to delay dependency injector resolution... but how?! it's not in the docs...</p>',
styleUrls: [],
providers: [
SomeService
]
})
export class MyComponent implements OnInit {
@Input() config: Config;
ngOnInit(): void {
// I need to modify the providers array of the injector at runtime:
// { provide: CONFIG_TOKEN, useValue: this.config }
// Any ideas on how to achieve this?
}
}
// some_service.ts
import { CONFIG_TOKEN } from 'injector_tokens';
export class SomeService {
constructor(@Inject(CONFIG_TOKEN) config: Config) {
// The config object should be the one passed in through the HTML attribute
}
}
Looking for advice on how to accomplish this task.