urish / angular-load

Dynamically load scripts and css stylesheets in your Angular.JS app
MIT License
243 stars 65 forks source link

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

Open hags37 opened 9 years ago

hags37 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

@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 commented 9 years ago

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.