错误:未定义$ http

时间:2015-01-27 19:10:18

标签: angularjs

我正在试图通过我自己的查询方法为客户列表创建一个控制器/服务。

这很奇怪,因为“getAll”方法工作得很好,问题是查询或其他一些get方法......无论如何,这是服务:

(function() {
  'use strict';

  angular
    .module('omi.services')
    .factory('cliente', clienteFactory);

    clienteFactory.$inject = ['$http'];

    function clienteFactory ($http) {
      var baseUrl = 'http://localhost:3000/omi/v1/clientes/';
      var service = {
        create  : create,
        getAll  : getAll,
        getOne  : getOne,
        query   : query,
        remove  : remove,
        update  : update
      };

      return service

      function create (clienteData) {
        // body...
      } // end create

      function getAll () {
        return $http.get(baseUrl)
          .success(getAllComplete)
          .error(getAllOnError);

        function getAllComplete (data) {
          return data;
        }
        function getAllOnError (data) {
          return data;
        }
      }
    } // end facturaFactory

    function getOne (cliente) {
      return $http.get(baseUrl + cliente)
        .success(getOneComplete)
        .error(getOneOnError);

      function getOneComplete (data) {
        return data;
      }
      function getOneOnError (data) {
        return data;
      }
    } // end getOne

    function query (query) {
      return $http.get(baseUrl + 'buscar' + query)
        .success(queryComplete)
        .error(queryOnError);

      function queryComplete (data) {
        return data;
      }
      function queryOnError (error) {
        return data;
      }
    } // end query

    function remove (factura) {
      // body
    } // end remove

    function update (clienteData) {
      // body...
    } // end update
})();

它尚未完成,但整个结构。所以,问题在控制器中引发:

(function(){
  'use strict';

  angular
    .module('omi.controllers')
    .controller('clientReportsResult', clientReports);

    clientReports.$inject = ['$stateParams', 'cliente'];

    function clientReports ($stateParams, cliente) {
      /* jshint validthis: true */
      var vm = this;
      vm.clientes = [];

      var id = $stateParams.data;
      var query = "?id=" + id;

      fire();

      function fire () {
        cliente.query(query).then(function(data) {
          vm.clientes = data.data.clientes;
        });
      }
    }
})();

它解雇了我这个追溯:

"Error: $http is not defined
query@http://localhost:8080/js/services/clientes.service.js:55:7
fire@http://localhost:8080/js/controllers/clientes.reports.results.js:21:9
clientReports@http://localhost:8080/js/controllers/clientes.reports.results.js:18:7
invoke@http://localhost:8080/js/libs/angular/angular.js:4182:14
instantiate@http://localhost:8080/js/libs/angular/angular.js:4190:27
$ControllerProvider/this.$get</<@http://localhost:8080/js/libs/angular/angular.js:8449:18
$ViewDirectiveFill/<.compile/<@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3897:28
invokeLinkFn@http://localhost:8080/js/libs/angular/angular.js:8213:9
nodeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7722:1
compositeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7075:13
publicLinkFn@http://localhost:8080/js/libs/angular/angular.js:6954:30
updateView@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3839:23
$ViewDirective/directive.compile/<@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3807:9
invokeLinkFn@http://localhost:8080/js/libs/angular/angular.js:8213:9
nodeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7722:1
compositeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7075:13
compositeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7078:13
publicLinkFn@http://localhost:8080/js/libs/angular/angular.js:6954:30
$ViewDirectiveFill/<.compile/<@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3905:9
invokeLinkFn@http://localhost:8080/js/libs/angular/angular.js:8213:9
nodeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7722:1
compositeLinkFn@http://localhost:8080/js/libs/angular/angular.js:7075:13
publicLinkFn@http://localhost:8080/js/libs/angular/angular.js:6954:30
updateView@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3839:23
$ViewDirective/directive.compile/</<@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3801:11
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@http://localhost:8080/js/libs/angular/angular.js:14702:15
transitionTo/$state.transition<@http://localhost:8080/js/libs/angular-ui-router/release/angular-ui-router.js:3218:11
processQueue@http://localhost:8080/js/libs/angular/angular.js:13170:27
scheduleProcessQueue/<@http://localhost:8080/js/libs/angular/angular.js:13186:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8080/js/libs/angular/angular.js:14383:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/js/libs/angular/angular.js:14199:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:8080/js/libs/angular/angular.js:14488:13
done@http://localhost:8080/js/libs/angular/angular.js:9646:36
completeRequest@http://localhost:8080/js/libs/angular/angular.js:9836:7
requestLoaded@http://localhost:8080/js/libs/angular/angular.js:9777:1
" "<div ui-view="" class="details ng-scope">"

我正在试图解决这个问题,但是我无法理解这个问题,为什么会出现这个错误?在我使用“getAll”方法的其他控制器中,一切正常!

1 个答案:

答案 0 :(得分:4)

getOne函数不在clienteFactory工厂函数之外,其中$http已定义,getAll在范围内。

这是你的结构:

function clienteFactory ($http) {
  function getAll () {
  }
}

function getOne () {
   // $http is not defined here
}

query相同。

getOnequery放入clienteFactory

function clienteFactory ($http) {
  function getAll () {
  }

  function getOne () {
  }

  function query() {
  }
}