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 think this can help you as much as it helped me: https://github.com/KillerCodeMonkey/ionic-starter-eventmaps-tablet
– Ivan Ferrer
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] ...
– Ivan Ferrer
if it is to display the error should be :
data.value
, that returns a collection:data.value[0], data.value[1] ...
– Ivan Ferrer
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.
– Ivan Ferrer
look at this example has everything you need: http://cacodaemon.de/index.php?id=51
– Ivan Ferrer
https://ajax.googleapis.com/ajax/services/feed/find?v=1.0&q=termo+Buscado
– Ivan Ferrer