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

栏目: JavaScript · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/29878771/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;
    });

}]);

您需要更改$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表示范围,

b是$http,

和c是$资源.

在你的情况下,它实际上是尝试“$resource.get”,因此给你错误.

进一步阅读检查给定文档页面上关于缩小的注释:

https://docs.angularjs.org/tutorial/step_05

翻译自:https://stackoverflow.com/questions/29878771/angularjs-controller-error-http-get-is-not-a-function-in-controller-section


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法Ⅰ-Ⅳ

算法Ⅰ-Ⅳ

塞奇威克 / 中国电力出版社 / 2003-11 / 70.00元

《算法I-IV(C实现):基础、数据结构、排序和搜索(第3版)(影印版)》实为一个卓越的读本,作为一个普通的程序员,如果在数学分析方面不算熟练,同时又对理论算法很感兴趣,那么这《算法I-IV(C实现):基础、数据结构、排序和搜索(第3版)(影印版)》确定不容错过,由此你将获益匪浅。Sedgewick擅长深入浅出的方式来解释概念,他在这方面确有天分。另外书中使用了一些实践程序,其篇幅仅有一页左右,而......一起来看看 《算法Ⅰ-Ⅳ》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试