Possible to use a custom Angular service before bootstrap?

拈花ヽ惹草 提交于 2020-01-01 06:12:09

问题


I have a service:

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

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

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

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?


回答1:


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();

See, also, this short demo.
As you can see the service is re-instantiated (so counter starts from 0) for use within the Angular app.



来源:https://stackoverflow.com/questions/23745397/possible-to-use-a-custom-angular-service-before-bootstrap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!