在一个服务 (Angular) 中引用多个 API 调用 [英] Referencing multiple API calls in one Service (Angular)

查看:42
本文介绍了在一个服务 (Angular) 中引用多个 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 Angular $http 请求访问 API,以收集不同足球队的信息.

I am accessing an API via Angular $http requests to gather information for different football teams.

如果我只访问一个团队,这会很好 - 我会创建一个进行调用的服务,然后在我的控制器中引用服务函数.但是,我想在众多团队中执行此操作,而不必为每个团队创建单独的服务模块.

If I were to be only accessing one team, this would be fine - I would create a Service that made the call, then reference the Service function in my controller. However, I want to do this on numerous teams, without having to create a separate Service module for each one.

服务

app.factory('APIService', ['$http',
   function($http) {
      return $http.get('http://API/team/1204?Authorization=xxxxx')
      .success(function(data) {
        return data;
      })
        .error(function(err) {
        return err;
      });
   }
]);

在我的控制器内部...

APIService.success(function(data) {
    $scope.apiData = data; 
});

正如您在服务中看到的,团队是特定的,1204",并且只会从该团队中提取数据.我想创建一个函数,允许根据团队的不同,这四位代码可以互换,但我不知道该怎么做,也不知道把它放在哪里.

As you can see in the Service, the team is specific, "1204", and will only pull in the data from that one team. I want to create a function that allows that four digit code to be interchangeable depending on the team, but I don't know how, or where to put it.

任何帮助将不胜感激.提前致谢.

Any help would be massively appreciated. Thanks in advance.

推荐答案

我构建了一个通用 Angular 服务,它是您的应用程序中唯一需要的服务.

I built a Generic Angular Service that is the only service you will need in your application.

https://github.com/cullimorer/AngularGenericAPIService

该服务包含许多不同的方法:

The service contains a number of different methods:

GET (数组) - GetListData

GET (array) - GetListData

GET - 获取数据

PUT - 更新数据

POST - 添加数据

POST - AddData

删除 - 删除数据

那么,这有什么特别之处?好吧,发生的情况是您可以调用任何端点,传入任意数量的参数.这很像 C# 中的string.Format"函数,它将获取指定对象的值并将它们插入到另一个字符串中.commonService 包含一个名为stringFormat"的方法,该方法复制此功能以供通用 API 服务使用.

So, what's special about this? Well, what happens is you can call any endpoint passing in as many parameters as you like. This works a lot like the "string.Format" function in C# where it will take the value of objects specified and insert them into another string. The commonService contains a method called "stringFormat" which replicates this functionality for use by the generic API service.

让我们看看我们如何在实践中做到这一点.如果您想调用一个名为fooBars"的静态 API 端点,传入一个ID"为 1 以返回单个fooBar",我们会这样做:

Let's see how we do this in practice. If you wanted to call a restful API endpoint called "fooBars" passing in an "ID" of 1 to return a single "fooBar", we would do it like so:

return genericService.getData('fooBars/{0}', [1]);

这将使用 URL 调用 API:

This will call the API with the URL:

"http://localhost/API/fooBars/1"

第二个参数是一个数组,这样您就可以将任意数量的参数传入字符串中,假设我们有许多foos"和bars",我们可能会这样做:

The second parameter is an array, this way you can pass in as many parameters as you like into the string, let's say we have a number of "foos" and "bars" we might do something like this:

return genericService.getData('foos/{0}/bars/{1}', [1, 2]);

这将使用 URL 调用 API:

This will call the API with the URL:

"http://localhost/API/foos/1/bars/2"

等等.这是一个非常简单的服务,但我在所有 AngularJS 项目中都使用它,因为它很容易实现,这意味着您不必在 Angular 服务中编写大量不同的查询或编写长字符串连接.

And so forth. It's a pretty simple service but I use it in all AngularJS projects because it's so easy to implement and means you don't have to write a tonne of different queries in your Angular services or write long string concatenations.

这篇关于在一个服务 (Angular) 中引用多个 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆