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

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

内容简介:翻译自: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


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

查看所有标签

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

新经济,新规则

新经济,新规则

[美]Kevin Kelly / 刘仲涛 / 译言·东西文库/电子工业出版社 / 2014-7 / CNY 45.00

近年来,互联网持续震动着全世界各个行业以至于整个经济规则……在中国,以小米为代表的各类“互联网思维”轰轰烈烈地颠覆着各个行业……而这一切的一切,凯文凯利早就通过这本小书轻松写定。《新规则,新经济》一书介绍互联网时代,互联网影响下的经济运行的十个新游戏规则。一起来看看 《新经济,新规则》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器