I built an application settings service for my angular app local storage stuff. I want to create, validate, get and set all top level local storage settings from here.
This works as expect so far, but now I want change detection in the service to listen for alterations to the setting config objects that exist in the service.
I basically want to use onChanges in a service.
For example:
SettingService:
constructor() {
this.set_AppSettings();
}
public app_Settings: AppSettingsConfig = {
appConfig: {
showSideBar: null,
collapsedSideBar: null,
lastRoute: null,
showAppIDBar: null,
isDarkTheme: null
}
};
// Methods that handle localstorage stuff.
AppComponent.ts:
constructor(
public _settings: SettingsService
) {}
toggle() {
_settings.app_Settings.appConfig.showSideBar = !_settings.app_Settings.appConfig.showSideBar
}
AppComponent.html:
<button mat-flat-button (click)="toggle()"
Toggle Side Bar
</button>
I want to detect that this value has changed in my Settings Service and then fire the appropriate method to set the new value to local storage.
I don't want to call the method that sets the new local storage value from my service, every time I change a variable in a component. I feel like that would be unnecessary if I can just listen to the changes in the service.
Is this achievable or should I change my approach?