angular-load icon indicating copy to clipboard operation
angular-load copied to clipboard

How do I use the methods inside dynamically added file?

Open hags37 opened this issue 11 years ago • 5 comments

This library is neat. But I have a question - how do I use the services inside a newly loaded angular file? They don't seem to be available for injection. thoughts?

hags37 avatar Dec 14 '14 04:12 hags37

Thanks, the library does not deal with this. One way you can achieve this is by manually querying the AngularJS injector from within the loaded script files:

var myService = angular.element(document.querySelector('ng-scope')).injector().get('myService');

This should do the trick and fetch an instance of a service called myService.

urish avatar Dec 14 '14 08:12 urish

Thanks Uri. If i try to use your command I get this error. https://docs.angularjs.org/error/$injector/unpr?p0=TestServiceProvider%20%3C-%20TestService

The trouble is there is no way to register the service beforehand as a dependency with my controller as it is loaded only after bootstrap. Let me know if you have any ideas.

hags37 avatar Dec 15 '14 02:12 hags37

Finally cracked it! the trick is to run your service inside an ngApp on the head. And manually bootstrap another ng-app on the body with all the dependencies wired in. Also, could you help create a version where I pass an array of srcs and all the files get attached?

hags37 avatar Dec 15 '14 06:12 hags37

@hags37 Perhaps you can create a small plunkr/pull request with an example, for the benefit of future users?

We can then put it into an 'examples' directory inside the repository...

Thanks!

urish avatar Dec 15 '14 09:12 urish

http://plnkr.co/edit/QrjykCQmm9PHzMRRY43b

For the sake of the plunker, i copied snippets from your code. But the general usage of your library is solid.

hasridharan avatar Dec 17 '14 00:12 hasridharan