在一个服务(角度)中引用多个API调用

编程入门 行业动态 更新时间:2024-10-26 14:28:54
本文介绍了在一个服务(角度)中引用多个API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

如果我仅访问一个团队,那很好-我将创建一个进行调用的Service,然后在我的控制器中引用Service函数.但是,我想在众多团队中做到这一点,而不必为每个团队创建单独的服务模块.

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('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.

github/cullimorer/AngularGenericAPIService

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

The service contains a number of different methods:

获取(数组)-GetListData

GET (array) - GetListData

GET-GetData

GET - GetData

PUT-UpdateData

PUT - UpdateData

POST-AddData

POST - AddData

删除-DeleteData

DELETE - DeleteData

那么,这有什么特别之处?好吧,发生的事情是您可以调用任何端点,只要您传入任意数量的参数即可.这与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:

" 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:

" 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.

更多推荐

在一个服务(角度)中引用多个API调用

本文发布于:2023-10-29 12:59:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539758.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   角度   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!