0

Still trying to learn Angular.

I have a plunker here - https://plnkr.co/edit/oEXn5JDKeqoN82jz3PC1?p=preview

Trying to load external json file using $http and $q

I have a simpleController calling a getJson() function in a simpleService file to load the json.

Just trying to output the data on the home page to start with.

Locally I'm getting an error in the console saying

        TypeError: simpleService.getJson is not a function

The service

(function() {

  angular.module('cxoAppJs').service('simpleService', simpleService);

  simpleService.$inject = ['$http', '$q'];

  function simpleService($http, $q) {

    function getJson() {

      var deferred = $q.defer();

      http.get('https://api.myjson.com/bins/gznzh').then(function() {

        deferred.resolve(data);

        return deferred.promise;

      })

    }

  }

})();

2 Answers 2

2

Like Tome Pejoski says, you dont need to wrap the $http.get function

but your error isnt a problem with the wrapping byself. You have to return your functions in a service to get access to it

(function(){
      angular.module('cxoAppJs').service('simpleService', simpleService);

      simpleService.$inject = ['$http', '$q'];
      function simpleService($http, $q){
        var service = {};
        service.getJson = function(){ return $http.get('https://api.myjson.com/bins/gznzh');};
        return service;
      }
})();

After that you can call it in your Controller

angular.module('cxoAppJs').controller('ExampleCtrl', ExampleCtrl);
ExampleCtrl.$inject('$scope', 'simpleService');
function ExampleCtrl($scope, simpleService){
  $scope.exampleData = {};
  simpleService.getJson().then(function(request){
     $scope.exampleData = request.data
  })
}

Edit:

Quick Example Plunker

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

8 Comments

got your Plunkter to work too: plnkr.co/edit/TOiRJzxdlb8fXgY4Pedi?p=preview Happy Coding!!
You have to include your separate JS files too, and I fixed some small issues with naming and typing
Thanks 42tg I can see how the plunker works but I was trying to do this with $q and defer. I know $http returns a promise but Ive seen examples using $q and I was trying to understand how to do that. I'll try and update my plunker and question.
here is an example how to do it with $q directly, added two ways of creating promises, choose what you like more and welcome to the promise chaining :)
Just to add an important notice the return $q((resolve,reject)=>{}) shorthand function only works with angular >= 1.3.0
|
1

You don't need to wrap the HTTP call in a promise. $http automatically creates a promise.

Your code should look like this:

$http.get('https://api.myjson.com/bins/gznzh').then(function(result){
  return result.data;       
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.