I have an Angular service/provider that serves json data to my controller which works great:
angular.module('myApp.services', ['ngResource']).
factory("statesProvider", function($resource){
return $resource('../data/states.json', {}, {
query: {method: 'GET', params: {}, isArray: false}
});
});
But I also need to serve json data to the same controller from another file counties.json
.
Where can I find out how to I write a service that serves both files to my controller?
You can update service to return a hash of resources, not a single one:
You will be able to use it adding
.query()
at the end your function name i.e.geoProvider.states.query()
andgeoProvider.countries.query()
andmyApp.services
has to be injected into your controller, then injectgeoProvider
service into controller itself as well.I'm assuming you want to execute some code when both files have loaded. Promises work really well for this. I don't think resources return promises, but you can use the $http service for simple ajax calls.
Here I define one service each for the two data files, and a third service that returns a promise that gets fulfilled when both files are done loading.
Then in your controller: