I am using angularjs, underscore and jQuery in my new service:
myModule.factory('MyService', ['MyResource', function (MyResource) {
....
// Here I make use of _ and $
}]);
How can I inject underscore or jQuery to the new service so I can be sure that _ is underscore and $ is jquery?
I am looking for something like:
myModule.factory('MyService', [ 'underscore', 'jquery','MyResource', function (_, $, MyResource) {
....
// Here I want to use $ and _ and be SURE that _ is underscore and $ is jquery
}]);
based on @moderndegree's approach I have implemented the following code, I cannot say it is perfect but this way tester would know if it has jQuery dependency as
$window
is too generic object to inject.If you include jQuery and Underscore in your HTML, they will be globally available. There is no need to "inject" them.
If you wanted to include them in a module, you could do something like this:
Doing this will allow you to load the libraries asynchronously while keeping them from conflicting with previously loaded versions. There may be a better way to store references to the loaded libraries but this should work just fine.
Also, this example relies on a third party laoder ($script.js).
And here is a jsFiddle (http://jsfiddle.net/moderndegree/bzXGx/);