I have a provider in AppModule which provides a class.
Is there a way to update the provided value at runtime?
{provide: MatDatepickerIntl, useClass: SomeClass}
How can replace SomeClass with AnotherClass at runtime (i.e.: when reacting to an event in a component).
I'm using Angular 9.
EDIT
I know could use useFactory to generate the provided value based on some logic. The problem with that approach is that the factory function still runs just once, when the component is created.
What I wanna do is run that function again whenever some event occurs. How can I do that?
EDIT2:
So app.module does this:
// this is in providers array...
{
provide: MatDatepickerIntl, deps: [DatePickerIntlService],
useFactory: (datePickerIntl) => datePickerIntl.getLocale()
}
The above service reads current locale and it returns a MatDatepickerIntl subclass instance accordingly, so it creates the correct class.
Finally I have the event in app component ngOnInit:
onLangChange.subscribe(() => {
})
How do I change the provided MatDatepickerIntl subclass in the event handler? I can call the service but there is no way to update the provided value with the result...
Material Date Picker expects MatDatepickerIntl token to return the desired subclass. That is how it loads localized texts.
Hope this clarifies things a bit better.
Thanks in advance!