Javascript AngularJS 控制器错误 - :$http.get 不是控制器部分中的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29878771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:11:39  来源:igfitidea点击:

AngularJS Controller Error - : $http.get is not a function in controller section

javascriptangularjsrestful-urlngresource

提问by Rajesh Kumar

var hsbc = angular.module('hsbc',['ngResource','ngRoute']);

hsbc.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider){

hsbc.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider){

//console.log('config part working'); 
$routeProvider
    .when('/login', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/authentication/views/login.html',
        hideMenus: true
    })
    .when('/gloabltranfer', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/home/views/gloabltranfer.html'
    })
    .when('/tranferReq', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/home/views/TransferRquest.html'
    })
    .when('/reviewdetail', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/home/views/Reviewdetails.html'
    })
    .when('/confirmdetail', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/home/views/confirmdetails.html'
    })

    .when('/', {
        controller: 'hsbccontroller',
        templateUrl: 'modules/authentication/views/login.html'
    })

    .otherwise({ redirectTo: '/login' });

}]).controller('hsbccontroller', ['$scope','$http','$resource', function($scope,$resource,$http){

}]).controller('hsbccontroller', ['$scope','$http','$resource', function($scope,$resource,$http){

    //console.log('controller part working'); 
$http.get('http://localhost:8080/1/').success(function(data) {
    alert(data);
        $scope.greeting = data;
    });

}]);

}]);

回答by Kop4lyf

You need to change the positions of $http and $resource.

您需要更改 $http 和 $resource 的位置。

How angularJS works is, (if defined in this way), angular tries to match the strings provide to the arguments of the function, so that it knows which argument is what. This is basically for the purpose of minification, which will actually change the variables like illustrated below.:

angularJS 的工作原理是(如果以这种方式定义),angular 尝试将提供的字符串与函数的参数进行匹配,以便它知道哪个参数是什么。这基本上是为了缩小的目的,这实际上会改变如下所示的变量:

.controller('hsbccontroller', ['$scope','$http','$resource', function(a,b,c){

    //console.log('controller part working'); 
a.get('http://localhost:8080/1/').success(function(data) {
    alert(data);
        $scope.greeting = data;
    });
}]);

so here, angularjs knows that:

所以在这里,angularjs 知道:

a means $scope,

a 表示 $scope,

b is $http,

b 是 $http,

and c is $resource.

c 是 $resource。

In your case, it was actually trying "$resource.get" and hence giving you the error. Further reading check the note on minification on the given doc page: https://docs.angularjs.org/tutorial/step_05

在您的情况下,它实际上是在尝试“$resource.get”,因此给了您错误。进一步阅读检查给定文档页面上关于缩小的说明:https: //docs.angularjs.org/tutorial/step_05

回答by HymanDan9

In my opinion, It is error location - .controller('hsbccontroller', ['$scope','$http','$resource', function($scope,$resource,$http).

在我看来,这是错误位置 - .controller('hsbccontroller', ['$scope','$http','$resource', function($scope,$resource,$http)

right location - .controller('hsbccontroller', ['$scope', '$http','$resource', function($scope, $http, $resource):

正确的位置 - .controller('hsbccontroller', ['$scope', '$http','$resource', function($scope, $http, $resource)

.controller('hsbccontroller', ['$scope', '$http','$resource', function($scope, $http, $resource){
    $http.get('http://localhost:8080/1/').success(function(data) {
    alert(data);
        $scope.greeting = data;
    });
}

I had a same problem like you, but use right location can solve it.

我和你有同样的问题,但使用正确的位置可以解决它。