本文介绍了AngularJS:为每个端点创建多个工厂?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据一些例子,我们似乎可以注入一个工厂,它包含一个像这样的休息服务的端点

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', ['$resource',
     function($resource) {
        return $resource('/recipes/:id', {id: '@id'});
}]);

这看起来不错,但假设我有其他端点,即/users/:id 和/groups/:id,因为您可以想象不同端点的数量会增加.

This looks great, but imagine I have other endpoints i.e. /users/:id, and /groups/:id, as you can imagine the number of different endpoints are going to increase.

因此,为每个端点使用不同的工厂是一种很好的做法,因此具有 ..

So it is good practice to have a different factory for each endpoint so having ..

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

services.factory('Users', ['$resource',.............

services.factory('Groups', ['$resource',...............

或者有其他推荐的方法吗?

Or is there another recommended way ?

我真的没有看到它有什么问题,但它会迫使我创建很多工厂来处理不同的端点.

I really don't see an issue with it but its going to force me to create a lot of factories just for dealing with the different endpoints.

非常感谢任何帮助或指导

Any help or guidance really apprecaited

谢谢

推荐答案

这是一个偏好问题.

但没有什么能阻止您将所有资源整合到一个工厂中:

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();
  ...
}

这篇关于AngularJS:为每个端点创建多个工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:54