I have a service which populates its fields values in Constructor. But I want the service's fields values to be updated whenever the service is invoked:
service.ts:
@Injectable()
export class MyDefaultIntl extends OwlDateTimeIntl {
rangeFromLabel = '';
constructor(
private translateService: TranslationService,
) {
super();
console.log('service is constructed');
this.subscribeToTranslations();
}
subscribeToTranslations(): void {
this.translateService
.translateKey('FROM_LABEL')
.subscribe(message => {
console.log('From Message:' + message);
this.rangeFromLabel = message;
});
}
}
In my module.ts:
@NgModule({
imports: [OwlDateTimeModule,OwlNativeDateTimeModule],
exports: [OwlDateTimeModule,OwlNativeDateTimeModule],
providers: [
{provide: OwlDateTimeIntl, useClass: MyDefaultIntl},
],
})
translation service:
@Injectable()
export class TranslationService {
constructor(private translate: TranslateService) {
}
translateKey(key: string, param?: any){
return this.translate.get(key, param);
}
}
Now what happens is the Constructor is called just once, and never constructed again (i.e. it never gets destroyed so that it can be constructed again).
Due to this, the rangeFromLabel
value does not change later in the application because the service constructor is not called.
Is there any way to change the field value at later stages after the service is constructed?
Or is there any way to destroy the service and reconstruct it?
I've added the service in the providers provide section, and not the component's providers section. Will this make any change?