I have two custom methods I'm setting up. One for Global communication throughout the app, and another to handle http requests. My http request module requires my GlobalService to operate. How do I pass my GlobalService provider into my CustomHTTP provider?
CustomHTTP
angular.module("customHTTPService", []).factory("$chttp", [
"$http", "GlobalService", function($http, $parse) {
var chttp;
return chttp = function(method, url) {
return $http({
method: method,
url: GlobalService.apiRoot + url
}).success(function(data, status, headers, config) {
return [data, status, headers, config];
}).error(function(data, status, headers, config) {
return [data, status, headers, config];
});
};
}
]);
My globalService provider
angular.module('GlobalService', []).factory("GlobalService", function($rootScope) {
var sharedService, _ref;
sharedService = {};
return sharedService.apiRoot = (_ref = window.location.hostname) === "0.0.0.0" || _ref === "localhost" ? "http://localhost:3000/api/" : "http://website.com/api/";
});
And finally.. my app instantiation
window.app = angular.module('web_client', ['GlobalService', 'customHTTPService'], function($routeProvider, $locationProvider) {});
So then to test this, I throw this in one of my controllers :
this.SettingsController = function($scope, $location, GlobalService, $http, $chttp) {
$.v = $chttp;
};
And then I try to run it in console..
$.v('get', '/index.html');
Which returns :
ReferenceError: GlobalService is not defined
Or even a little more upstream, if I put this within my customHTTPService module.. :
console.log(GlobalService);
I get the same error.
What's the proper way to pass modules to one another? Is there something I could be doing better?
Your customHTTPService module needs to list the GlobalService module as a dependency.
angular.module("customHTTPService", ['GlobalService'])
However, first you need to change so that your GlobalService is defined before customHTTPService, otherwise the dependency will fail.
You can read more about modules and dependencies here.