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

Rajesh Kumar picture Rajesh Kumar · Apr 26, 2015 · Viewed 25.1k times · Source
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;
    });

}]);

Answer

Kop4lyf picture Kop4lyf · Apr 26, 2015

You need to change the positions of $http and $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.:

.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:

a means $scope,

b is $http,

and c is $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