gpt4 book ai didi

javascript - Angular 应用程序 http 调用自定义服务失败

转载 作者:行者123 更新时间:2023-11-29 16:47:39 26 4
gpt4 key购买 nike

我正在制作一个棱 Angular 分明的小应用程序来显示有关英超联赛的信息。我想提供一项服务来处理 http 调用,因为我在页面上做了几次,我不想重复所有的事情。这是我的 table.js TableController,用于构建排行榜/

(function(){

angular
.module("eplApp")
.controller("tableCtrl", TableController);

TableController.inject = ['httpService'];

function TableController(service){
var apiUrl = "http://api.football-data.org/v1/soccerseasons/426/leagueTable";

service.getListFromUrl(apiUrl).then(function(data){
var vm.this;
vm.data = response.data;
});
}
})();

这是我为运行 http 请求而创建的服务,service.js:

(function(){

angular
.module("eplApp")
.factory("httpService", httpService);

httpService.inject = ['$http'];

function httpService($http){
var apiKey = '971acba677654cdb919a7ccebd5621e2';
var vm = this;
vm.data = [];

$http({
headers: { 'X-Auth-Token': apiKey },
method: "GET",
url: apiUrl
}).then(function(response) {
vm.data = response.data;
return vm.data;
});
}
})();

当我运行它时,出现以下错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=serviceProvider%20%3C-%20service%20%3C-%20tableCtrl

我哪里出错了?

最佳答案

查看你使用的controller,controller函数参数是httpService,你在里面使用的是HttpService。请检查,区分大小写。

关于javascript - Angular 应用程序 http 调用自定义服务失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39505290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com