问题描述
下面的一些例子,我们似乎可以注入一个工厂,将包含一个端点,像这样一个REST服务
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'});
}]);
这看起来不错,但可以想象我有其他端点,即/用户/:id和/团体/: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.
任何帮助或指导真的AP precaited
Any help or guidance really apprecaited
感谢
推荐答案
这是preference的问题。
It's a matter of preference.
但是,从整合所有的资源的一个工厂里面在没有prevents你:
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:为每个端点创建多个工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!