Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

How do I use the methods inside dynamically added file? #9

Open
hags37 opened this issue Dec 14, 2014 · 5 comments
Open

How do I use the methods inside dynamically added file? #9

hags37 opened this issue Dec 14, 2014 · 5 comments

Comments

@hags37
Copy link

hags37 commented Dec 14, 2014

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?

@urish
Copy link
Owner

urish commented Dec 14, 2014

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.

@hags37
Copy link
Author

hags37 commented Dec 15, 2014

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
Copy link
Author

hags37 commented Dec 15, 2014

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?

@urish
Copy link
Owner

urish commented Dec 15, 2014

@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!

@hasridharan
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants