0

I have 3 files:

app.js

angular.module("starweb", ["ngRoute", "ngAnimate", "ui.bootstrap"])
       .config(function ($routeProvider) {
       $routeProvider  ....

addHostingController.js

  angular.module("starweb")
  .controller("addHostingCtrl", function ($scope, domainService) {
        $scope.data.domains = domainService.getDomain();
}

domainService.js

angular.module("starweb")
.constant("domainList", "http://localhost:15536/api/product/xxxx")
.service("domainService", function ($http, $q,domainList) {
    return ({
        getDomain: GetDomains()
    });
});

function GetDomains() {
    $http.get(domainList).then(handleSuccess, handleError);
}

On the page source, all 3 files are included (app.js first). Chrome shows $http is undefined, what's wrong with my setup ?

Thanks all.

1 Answer 1

2

The GetDomains function does not have access to the domainService scope, where you have injected the $http service.

You should change your code to something like this :

.service("domainService", function ($http, $q, domainList) {
  return {
    getDomain: function (handleSuccess, handleError) {
      $http.get(domainList).then(handleSuccess, handleError);
    }
  };
});

You should also define the handleSuccess and the handleError callbacks, surely as parameters of your getDomain function.

Sign up to request clarification or add additional context in comments.

1 Comment

$http get is a promise. You should return the promise, not map it to function delegates

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.