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