I have an object in a service, testProfile, which changes by many components. I want to call a method, saveToLocalStorage() from another service to save the changes of the testProfile object. How can I detect when the change happens in testProfile?
My code so far:
@Injectable()
export class ProfileService {
testProfile: ProfileInterface;
private profileChangeWatcher = new Subject<ProfileInterface>();
constructor(private localStorageService: LocalStorageService ) {
this.testProfile = this.createProfile(this.localStorageService.loadProfile('testProfile'));
}
saveProfileChanges() {
this.profileChangeWatcher.next(this.testProfile);
this.localStorageService.saveToLocalStorage(this.testProfile);
}
createProfile(profileData: ProfileInterface): ProfileInterface {
return new ProfileModel().init(profileData);
}
}
saveToLocalStorage
function just use another Observable which will emit the latest datathis.profileChangeWatcher.subscribe((value)=> { this.localStorageService.saveToLocalStorage(this.testProfile); })
Where should i call the saveProfileChanges() method?