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

Raj*_*mar 7 javascript restful-url angularjs ngresource

var hsbc = angular.module('hsbc',['ngResource','ngRoute']);
Run Code Online (Sandbox Code Playgroud)

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' });
Run Code Online (Sandbox Code Playgroud)

}]).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;
    });
Run Code Online (Sandbox Code Playgroud)

}]);

Kop*_*lyf 28

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

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;
    });
}]);
Run Code Online (Sandbox Code Playgroud)

所以在这里,angularjs知道:

a表示$ scope,

b是$ http,

和c是$资源.

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