简体   繁体   English

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

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

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

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){

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

}]);

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. angularJS 的工作原理是(如果以这种方式定义),angular 尝试将提供的字符串与函数的参数进行匹配,以便它知道哪个参数是什么。 This is basically for the purpose of minification, which will actually change the variables like illustrated below.:这基本上是为了缩小的目的,这实际上会改变如下所示的变量:

.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.在您的情况下,它实际上是在尝试“$resource.get”,因此给了您错误。 Further reading check the note on minification on the given doc page: https://docs.angularjs.org/tutorial/step_05进一步阅读检查给定文档页面上关于缩小的说明: https : //docs.angularjs.org/tutorial/step_05

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. 我和您一样有相同的问题,但是使用正确的位置可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM