Angularjs: a Service that serves multiple $resource urls / data sources?

后端 未结 2 1447
情深已故
情深已故 2020-11-30 19:05

I have an Angular service/provider that serves json data to my controller which works great:

    angular.module(\'myApp.services\', [\'ngResource\']).
               


        
2条回答
  •  感情败类
    2020-11-30 19:25

    You can update service to return a hash of resources, not a single one:

    angular.module('myApp.services', ['ngResource']).
      factory("geoProvider", function($resource) {
        return {
          states: $resource('../data/states.json', {}, {
            query: { method: 'GET', params: {}, isArray: false }
          }),
          countries: $resource('../data/countries.json', {}, {
            query: { method: 'GET', params: {}, isArray: false }
          })
        };
      });
    

    You will be able to use it adding .query() at the end your function name i.e. geoProvider.states.query() and geoProvider.countries.query() and myApp.services has to be injected into your controller, then inject geoProvider service into controller itself as well.

提交回复
热议问题