AngularJS: Creating multiple factories for every endpoint?

后端 未结 1 1797
难免孤独
难免孤独 2020-12-07 10:28

following some examples, it appears that we can inject a factory which would contain an endpoint for a rest service like so

services.factory(\'Recipe\', [\'$         


        
相关标签:
1条回答
  • 2020-12-07 10:43

    It's a matter of preference.

    But nothing prevents you from consolidating all your resources inside one factory as in:

    services.factory('Api', ['$resource',
     function($resource) {
      return {
        Recipe: $resource('/recipes/:id', {id: '@id'}),
        Users:  $resource('/users/:id', {id: '@id'}),
        Group:  $resource('/groups/:id', {id: '@id'})
      };
    }]);
    
    function myCtrl($scope, Api){
      $scope.recipe = Api.Recipe.get({id: 1});
      $scope.users = Api.Users.query();
      ...
    }
    
    0 讨论(0)
提交回复
热议问题