AngularJS $http.get 返回 undefined 并且 $http() 不是函数 [英] AngularJS $http.get returns undefined and $http() is not a function

查看:25
本文介绍了AngularJS $http.get 返回 undefined 并且 $http() 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序以在 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).

这篇关于AngularJS $http.get 返回 undefined 并且 $http() 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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