How can TypeScript be used to save data to a service?
Consider this example service:
class MyService {
persistentData: Object;
constructor() {
this.init();
}
private init = (): void => {
this.persistentData = {};
}
public saveData = (data): Object {
this.persistentData = data;
}
}
Is there a way to maintain consistent data across different routes? Each time a new route is accessed, the service class is instantiated again and the persistentData object is reset. In regular JavaScript, the following approach could be taken:
angular
.factory('myFactory', function() {
var persistentData = {};
return {
setFormData: function(newData) {
persistentData = newData;
}
}
})
While this method works, I feel like there might be something missing. Can anyone offer some insight?