Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i can reload/restart service in angular?

How can I restart a service without reloading an app?

I do not want to use window.location.reload().

like image 832
Natalia Konohov Avatar asked Jun 06 '17 07:06

Natalia Konohov


People also ask

What is live reload angular?

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.

How do you refresh a component from another component in angular 9?

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.


1 Answers

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

        };

    }

}
like image 179
Vladimir Zdenek Avatar answered Nov 05 '22 20:11

Vladimir Zdenek