How to make an http request in Angular, and have functions that return all JSON elements or just one passed by parameter?

Asked

Viewed 1,597 times

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 the other 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 the only "value" console

  • I think this can help you as much as it helped me: https://github.com/KillerCodeMonkey/ionic-starter-eventmaps-tablet

  • you can access the property seuobjeto.value[0], in case I believe it is: noticia.value, that returns a collection: noticia.value[0], noticia.value[1] ...

  • if it is to display the error should be : data.value, that returns a collection: data.value[0], data.value[1] ...

  • your method has some things kind of unnecessary... kind of what to do a for and get out on the first call? just do this: news[0]. id, that I know all ids will be whole. and for query, pass id as parameter to search only that news.

  • 1

    look at this example has everything you need: http://cacodaemon.de/index.php?id=51

  • https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=termo+Buscado

Show 1 more comment

1 answer

2

Solution Found : Based on: https://stackoverflow.com/a/33023283/5424391

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)];        
    });
  }
  };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.