How to make an HTTP request in the Angular, and have functions that return all JSON elements or just one last parameter?

6

I'm new to Angular and Ionic, and I want to build a factory that gets a JSON from googleapis, and contains two functions, one returns all elements, and another that returns the element that is passed the index by parameter.

I'm trying, like this:

Factory:

angular.module('starter.services', [])

.factory('Noticias', function($http,$q) {
  var deferred = $q.defer();                
        $http.get("http://ajax.googleapis.com/ajax/services/feed/load", { params: { "v": "1.0", "q": "http://www.furg.br/bin/rss/noticias.php", "num":"10" } })
          .success(function(data) {
              entries = data.responseData.feed.entries;
              deferred.resolve(entries);
          })
          .error(function(data) {
              console.log("ERROR: " + data);
          });

  var noticias =  deferred.promise;
  console.log(noticias);
  return {
    all: function() {
      return noticias;
    },
    remove: function(noticia) {
      noticias.splice(noticias.indexOf(noticia), 1);
    },
    get: function(noticiaId) {
      for (var i = 0; i < noticias.length; i++) {
        if (noticias[i].id === parseInt(noticiaId)) {
          return noticias[i];
        }
      }
      return null;
    }
  };
});

I got this on the console, but I want it just the "value"

    
asked by anonymous 08.10.2015 / 19:33

1 answer

2

Solution Found: Based on: link

angular.module('starter.services', [])

.factory('Noticias', function($http,$q) {

  var noticias = $http.get("http://ajax.googleapis.com/ajax/services/feed/load", { params: { "v": "1.0", "q": "http://www.furg.br/bin/rss/noticias.php", "num":"20" } })
    .then(function(response) {
        return response.data.responseData.feed.entries;
    });

  return {
    all: function() {
      return noticias.then(function(array){
        return array;
      });
    },
    get: function(noticiaIndex) {
    return noticias.then(function(array) {
        return array[parseInt(noticiaIndex)];        
    });
  }
  };
});
    
08.10.2015 / 22:58