可以在引导之前使用自定义 Angular 服务吗? [英] Possible to use a custom Angular service before bootstrap?

查看:24
本文介绍了可以在引导之前使用自定义 Angular 服务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项服务:

angular.module('USC').service('TemplateService', function() {});

在我手动引导我的 Angular 项目之前我想使用的:

That I would like to use before I manually bootstrap my Angular project:

angular.bootstrap(document, ['USC']);

这可能吗?我知道我可以调用 var service = angular.bootstrap().get();对于 Angular 提供的服务,但如何在模块初始化之前调用自定义服务?

Is this possible? I know I can call var service = angular.bootstrap().get(); for Angular provided services, but how would I call a custom one before the module was initialized?

推荐答案

如果可以使用与应用程序本身使用的服务实例不同的服务实例,您可以像这样实现您想要的:

If it is OK to have a different instance of the service than the one that will be used by the app itself, you can achieve what you want like this:

angular.injector(['ng', 'yourApp']).get('SomeService').doStuff();

另请参阅此简短演示.
<子>如您所见,该服务被重新实例化(因此计数器从 0 开始)以在 Angular 应用程序中使用.

这篇关于可以在引导之前使用自定义 Angular 服务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆