angularjs函数($ http,$ location,...)不起作用

时间:2017-03-24 12:17:38

标签: angularjs http

我有一个简单的angularjs应用程序,我想发出http请求,但我真的不知道为什么它不起作用。这是我的控制器:

(function () {
    'use strict';

        /**
        * @ngdoc function
        * @name app.controller:HomeCtrl
        * @description
        * # HomeCtrl
        * Controller of the app
        */

      angular.module('BlurAdmin.pages.dashboard')
      .controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location', HomeCtrl ]);
        function HomeCtrl($scope, $http, $location) {
          $scope.samples = [['a', 'b', 'c'], ['d', 'e', 'f']];

        console.log($http);
        console.log($scope);
        console.log($location);
        console.log(window.location.hostname);
        $http.get(
          'http://localhost' +   ':7000/samples', 

          {}
        ).then(function(err, data){
          if (err){
            console.log(err);
          }
          console.log(data);
        },
        function(err, data){
        });

            var varApplist = this;
        }
    })();

我的家庭主持人:

<div id="page-wrapper" ng-controller="HomeCtrl">
......
<div>

在我的index.html

当我尝试运行时,我遇到了这个错误:

TypeError: $http.get is not a function
    at new HomeCtrl (http://localhost:4001/app/pages/dashboard/home/homeCtrl.js:21:11)
    at invoke (http://localhost:4001/bower_components/angular/angular.js:4570:17)
    at Object.instantiate (http://localhost:4001/bower_components/angular/angular.js:4578:27)
    at http://localhost:4001/bower_components/angular/angular.js:9460:28
    at http://localhost:4001/bower_components/angular-ui-router/release/angular-ui-router.js:4081:28
    at invokeLinkFn (http://localhost:4001/bower_components/angular/angular.js:9099:9)
    at nodeLinkFn (http://localhost:4001/bower_components/angular/angular.js:8586:11)
    at compositeLinkFn (http://localhost:4001/bower_components/angular/angular.js:7975:13)
    at publicLinkFn (http://localhost:4001/bower_components/angular/angular.js:7855:30)
    at updateView (http://localhost:4001/bower_components/angular-ui-router/release/angular-ui-router.js:4021:23) <div ui-view="" class="ng-scope">

当我记录$ location和$ http时,我有空对象。

2 个答案:

答案 0 :(得分:7)

.controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location', HomeCtrl ]);
        function HomeCtrl($scope, $http, $location) {

除了$scope之外,函数的实际参数根本不匹配在数组开头声明的依赖项。

帮自己一个忙,并停止使用这个丑陋且容易出错的数组表示法。使用ng-annotate可以自动修改代码。

另请注意,传递给then()的回调将使用单个参数调用。所以你在那里的data参数是没用的。

答案 1 :(得分:1)

需要将依赖项注入序列与字符串值匹配

.controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location',
            function HomeCtrl($scope, $rootScope,$stateParams, $http, $location) {

初始化角度模块时,需要为模块注入添加空的squire支架

angular.module('BlurAdmin.pages.dashboard',[])

还会在您的承诺err

中删除(then)
.then(function(data) {
        console.log(data); // success response
    },
    function(data) {
        console.log(data); // error resoponse
    });

完全控制器

(function () {
    'use strict';

      angular.module('BlurAdmin.pages.dashboard',[])
      .controller('HomeCtrl', ['$scope', '$rootScope', '$stateParams', '$http', '$location',
        function HomeCtrl($scope, $rootScope,$stateParams, $http, $location) {
          $scope.samples = [['a', 'b', 'c'], ['d', 'e', 'f']];

        console.log($http);
        console.log($scope);
        console.log($location);
        console.log(window.location.hostname);
        $http.get(
          'http://localhost' +   ':7000/samples', 

          {}
        ).then(function(data){

          console.log(data);
        },
        function(err){
            console.log(err);
        });

            var varApplist = this;
    }])


    })();