1

I am trying to get Json data from REST Api ("http://jsonplaceholder.typicode.com/posts/") using angularJS custom service but it is not showing the data. Although when I am directly typing the url in the browser it shows some data.

Here is the angular service code.

angular.module('myapp.service',[])
       .service('testService', function ($http) {

     //get All NewsLetter
     this.getPosts = function () {
         return $http.get('http://jsonplaceholder.typicode.com/posts');
     };
     });

and controller

angular.module('myapp.controller',[])
       .controller('testController',function($scope,testService){

        $scope.posts={};
 function GetAllPosts() {
               var getPostsData = testService.getPosts();

               getPostsData.then(function (post) {
                   $scope.posts = post.data;

               }, function () {
                   alert('Error in getting post records');
               });
           }
       });

When I debugged it in the browser . it shows service function is returning undefined data.

Although when I tried to get the data directly in the controller using the code

angular.module('myapp.controller',[])
       .controller('testController',function($scope,testService,$http){
         $http.get('http://jsonplaceholder.typicode.com/posts')
        .success(function (data) {
        $scope.posts=data ;
    });  

       });

it works fine,Can any one please tell me where i am making the mistake in the service.

I have used a parent module having all the dependency injections

angular.module('myapp',['myapp.controller','myapp.service']);  
3
  • Have you enabled cors on your server? Commented Mar 29, 2016 at 13:30
  • 1
    you forgot to import the myapp.service module into your myapp.controller module. That's why testService cannot be resolved and doesn't work. Add it as a dependency: angular.module('myapp.controller',['myapp.service']).Although I do wonder why you're creating 2 seperate modules for this? Commented Mar 29, 2016 at 13:31
  • @fikkatra I have used a parent module which has all the dependency injection.Sorry for not showing the whole code in the question initially. I have made the changes in the question. So dependency injection is not the issue for sure. Commented Mar 29, 2016 at 13:38

2 Answers 2

1

In the code you shared, you never invoked the GetAllPosts method in the controller. See working plunker below.

https://plnkr.co/edit/YSTWwS?p=preview

P.S. - I had to make the URL https for plunker

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

5 Comments

I have used a parent module which has all the dependency injection.Sorry for not showing the whole code in the question in the beginning. I have made the changes in the question. So dependency injection is not the issue here I guess.
Is it that you also did not invoke the GetAllPosts method in your controller?
@simba - Don't you hate the small things when you clearly understood the hard parts like DI :)
@simba, also, while the dependency injection you added does work, it can bite you in the butt one day because your true dependency for myapp.service is myapp.controller. There is no harm in putting the dependency in the myapp and myapp.controller modules since angular will only create a single instance of myapp.service which is shared between the two items.
Thank you for the suggestion.I will make the necessary changes and keep in mind in future.
0
service function is returning data, you can check by adding another then() to promise object returned by service.
function GetAllPosts() {
var getPostsData = testService.getPosts();
getPostsData.then(function (post) {
$scope.posts = post.data;
}, function () {
alert('Error in getting post records');
}).then(function(){
console.log($scope.posts); 
});
}
make sure that you are checking response inside then()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.