user6321478
user6321478

Reputation:

$http TypeError: Cannot read property 'get' of undefined - in a factory

Not sure why this is a problem

I have get error on $http

Index.html // References to files

<script src="common/services/common.services.js"></script>
<script src="common/services/deviceService.js"></script>

common.services.js // I'm not using ngResource /$resource for $http obviously do I need to use httpprovider ?

(function () {
      "use strict";

angular
    .module("common.services",   // name of module
    ["ngResource"])   // array that defines the dependencies  ( ngResource contains the $resource service)
} ());

PROBLEM is here...

deviceService.js

(function () {
"use strict";

angular
    .module("common.services")
    .factory("deviceService",
    ["$http",deviceService]);

    function deviceService($http) {

       function httpGetCost(cost, amount, $http) {
           $http.get('../api/deviceEvents.json')
            .then(function (result) {
           //.......
       }

       return {  outsideCall: httpGetCost }
   }

} ());
  1. I'm not sure purpose of common.service.js really...
  2. Do I need $http to have some httpprovider service reference?
  3. I gather I'm not injecting in $http correctly?

Upvotes: 0

Views: 313

Answers (1)

CozyAzure
CozyAzure

Reputation: 8478

Because you did not inject your $http properly - just a minor syntax error.

(function() {
    "use strict";

    angular
      .module("common.services")
      .factory("deviceService", ["$http", deviceService]);

    function deviceService($http) { //this is the real $http that gets injected

      function httpGetCost(cost, amount) { //remove your $http here
        $http.get('../api/deviceEvents.json')
          .then(function(result) {
              //.......
            }

            return {
              outsideCall: httpGetCost
            }
          }

      }());

Upvotes: 1

Related Questions