Search code examples
javascriptangularjsionic-frameworkangularjs-factoryangularjs-http

multiple returned keys json


I have a joomla site running k2 and pulling info from it by adding format=json to the end of the string: https://www.example.com/posts?format=json which outputs something like this:

{
site: {
      url: "https://www.example.com",
      name: "mySite"
      },
category: {
          id: "67",
          name: "Gauteng",
          alias: "gauteng",
          link: "/tna/provincial/gauteng.html",
          parent: "66",
          extraFieldsGroup: "0",
          image: null,
          ordering: "1",
        events: {
          K2CategoryDisplay: ""
        },
        chidlren: []
       },
items: [{ 
         id="1",
         title="The Title",
         body="body content here..."
        },
        { 
         id="2",
         title="The Title",
         body="body content here..."
        }
}

now i am creating a service for this and just want to access "items".

.factory('Posts', function($http) {

  var posts = [];

  return {
        getPosts: function(){
            return $http.get("https://www.example.com/posts?format=json").then(function(response){
                posts = response;
                return posts;
            });
        },
        getPost: function(index){
            return posts[index];
        }
    }

});

it doesnt seem to be working though. is there any way to access just "items" with the call?


Solution

  • You post syntax should be posts = response.data.items

    return $http.get("https://www.example.com/posts?format=json").then(function(response){
        posts = response.data.items; //items here
        return posts;
    });