0

I am trying to pull json data from the angular service but its always printing blank spaces, can some one help me please. No errors in the console as well.

Service

    app.service("Java4sService", function() {

     var details = {};
     this.details = function() {

         [{
             name: "James",
             country: "United Stages"
         }, {
             name: "Rose",
             country: "United Kingdom"
         }, {
             name: "Smith",
             country: "United States"
         }]

     };

     this.getDetails = function() {
         return details;
     };

 });

Controller

app.controller("Java4sController",function($scope,Java4sService){           
                $scope.personDetails = Java4sService.getDetails();
}); 

Html

<ul>
    <li ng-repeat="person in personDetails ">
            {{person.name}} - {{person.country}}
    </li>
</ul>
1
  • The problem is that in your service you are setting 'this.details' to a function which returns nothing. Look at your function more carefully again. As the answer recommends remove the function surrounding the array and then everything works fine. Commented Oct 15, 2014 at 3:56

1 Answer 1

1

You should modify your service as below:

app.service("Java4sService", function() {
    var details = [ {
        name: "James",
        country: "United Stages"
    }, {
        name: "Rose",
        country: "United Kingdom"
    }, {
        name: "Smith",
        country: "United States"
    } ]

    return {
        getDetails: function() {
            return details;
        }
    };
});

Here is the demo

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

3 Comments

I am using service, not factory. So we should use this.getDetails i guess.
One more thing.., what the difference between.. getDetails = function() {} and getDetails:function() {} ?
getDetails = function()... means you assign a Function object to variable getDetails. { getDetails: function()... } means you assign a Function object to property getDetails of an object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.