How can I restart a service without reloading an app?
I do not want to use window.location.reload()
.
Live reloading is a feature in Angular. The ng serve command starts a development server which listens to http://localhost:4200 and watches file changes in the src folder. Every file changes will trigger a reload.
To refresh, or better to say update another component from a different component, we can use the concept of Observables and Subject (which is a kind of Observable). This concept has an added benefit when data are to be received from APIs for CRUD operations.
You could use a factory instead of simple service and create a reset
method on it which would set it to its "initial state".
For example, your factory could look something like this:
function MyFactory() {
// generate the initial structure of the service
let Factory = _getDefaultFactory();
// expose the service properties and methods
return Factory;
/**
* Reset the factory back to its default state
*/
function reset() {
// generate the initial structure again
// and override the current structure of the service
Factory = _getDefaultFactory();
}
/**
* Generate a default service structure
*/
function _getDefaultFactory() {
return {
// include the reset method so you can call it later
// e.g. MyFactory.reset();
reset: reset
// include other factory properties and methods here
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With