AngularJS $http.get 返回 undefined 并且 $http() 不是函数

2022-01-24 00:00:00 angularjs javascript coffeescript

我正在构建一个应用程序以在 AngularJS 中动态加载和显示数据库中的数据,但是当我尝试访问我的 API(使用 $http() 或 $http.get())时,我收到了错误.$http.get() error: TypeError: undefined is not a function, $http() error: TypeError: object is not a function

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

此特定错误发生在动态加载导航选项卡的代码中.

This specific error occurs in the code to dynamically load navigation tabs.

CoffeeScript 中的代码:

The code for both in 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?

推荐答案

当使用数组表示法注入依赖时,参数的顺序很重要:

When using the array notation for injecting dependencies, the order of the arguments is important:

在您的代码中:

['$routeParams',
 '$scope',
 '$http',
 function ($http, $scope, $routeParams)  {
    // $http argument        ==> $routeParams
    // $scope argument       ==> $scope (by coincidence)
    // $routeParams argument ==> $http
}

所以,基本上,您正在执行 $routeParams.get(),但 $routeParams 没有方法 get()(也没有它本身就是一个函数).

So, basically, you are doing $routeParams.get(), but $routeParams has no method get() (nor is it a function itself).

相关文章