javascript AngularJS $http.get 返回 undefined 并且 $http() 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24123589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS $http.get returns undefined and $http() is not a function
提问by RogerCapiche
I'm building an app to dynamically load and display data from a database in AngularJS, but when I try to access my API (using either $http() or $http.get()), I receive errrors.
$http.get() error: TypeError: undefined is not a function
, $http() error: TypeError: object is not a function
我正在构建一个应用程序来动态加载和显示来自 AngularJS 数据库中的数据,但是当我尝试访问我的 API(使用 $http() 或 $http.get())时,我收到错误。$http.get() 错误: TypeError: undefined is not a function
, $http() 错误:TypeError: object is not a function
This specific error occurs in the code to dynamically load navigation tabs.
此特定错误发生在动态加载导航选项卡的代码中。
The code for both in CoffeeScript:
CoffeeScript 中两者的代码:
p4pControllers.controller 'navCtrl', [
'$routeParams'
'$scope'
'$http'
($http,$scope,$routeParams) ->
$http(
method:'GET'
url:'http://p4p-api.snyx.nl/tables'
).success (data) ->
$scope.tabs = data
return
return
$http.get('http://p4p-api.snyx.nl/tables').success (data) ->
$scope.tabs = data
return
return
]
Does anyone see what I'm doing wrong here?
有没有人看到我在这里做错了什么?
回答by gkalpak
When using the array notation for injecting dependencies, the order of the arguments is important:
使用数组表示法注入依赖项时,参数的顺序很重要:
In your code:
在您的代码中:
['$routeParams',
'$scope',
'$http',
function ($http, $scope, $routeParams) {
// $http argument ==> $routeParams
// $scope argument ==> $scope (by coincidence)
// $routeParams argument ==> $http
}
So, basically, you are doing $routeParams.get()
, but $routeParams
has no method get()
(nor is it a function itself).
所以,基本上,你正在做$routeParams.get()
,但$routeParams
没有方法get()
(它本身也不是一个函数)。