我如何以角度重新加载/重新启动服务?

Nat*_*hov 4 angularjs angular-services

如何在不重新加载应用程序的情况下重新启动服务?

我不想用window.location.reload().

Vla*_*nek 5

您可以使用工厂而不是简单服务,并reset在其上创建一个方法,将其设置为"初始状态".

例如,您的工厂可能看起来像这样:

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

        };

    }

}
Run Code Online (Sandbox Code Playgroud)