【问题标题】:AngularJS Controller Error - : $http.get is not a function in controller sectionAngularJS 控制器错误 - :$http.get 不是控制器部分中的函数
【发布时间】:2015-07-04 21:42:20
【问题描述】:
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;
    });

}]);

【问题讨论】:

  • 您的控制器参数的顺序与使用数组表示法声明的顺序不同。改变$resource$http的位置。
  • 应该是 .controller('hsbccontroller', ['$scope','$http','$resource', function($scope,$http, $resource){
  • 酷,感谢它的工作伙伴。使用 Rest API 在 angularJs 中工作是相当新的。非常感谢。

标签: javascript angularjs restful-url ngresource


【解决方案1】:

你需要改变$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;
    });
}]);

所以在这里,angularjs 知道:

a 表示 $scope,

b 是 $http,

c 是 $resource。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 1970-01-01
    • 2014-05-02
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    相关资源
    最近更新 更多