I would like to resolve a configuration object for the injector when ngOnInit
is called.
This is what I am trying to do:
<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 want to be able to now add to the injector's providers array:
// { provide: CONFIG_TOKEN, useValue: this.config }
// how can I do that?
}
}
// some_service.ts
import { CONFIG_TOKEN } from 'injector_tokens';
export class SomeService {
constructor(@Inject(CONFIG_TOKEN) config: Config) {
// config should be the config object passed in via html attribute
}
}
How can I do it?