如何在 Angular 中执行 http 请求,并具有 returns Json 的所有元素或只有一个元素的功能?

How can I do http request in Angular, and have functions that returns all elements of Json or just one?

我是 Angular 和 Ionic 的新手, 我想建立一个工厂,从 googleapis 获得一个 Json, 并包含两个函数,一个 returns 所有元素,另一个 returns 在参数中传递索引的元素。

我正在这样尝试:

工厂:

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

我在控制台中得到了这个,但我只想要控制器上的 "value"。

Promise {$$state: Object, then: function, catch: function, finally: function}$$state: Object
status: 1
value: Array[10]
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
6: Object
7: Object
8: Object
9: Object
length: 10
__proto__: Array[0]
__proto__: Object
__proto__: Object

noticias是一个承诺。你所有的方法都把它当作一个数组来使用。不是。这是一个承诺。

因此,方法 get 例如应该是

get: function(noticiaId) {
    return noticias.then(function(array) {
        for (var i = 0; i < array.length; i++) {
            if (array[i].id === parseInt(noticiaId)) {
                return array[i];
            }
        }
        return null;
    });
}

get() 方法的用户应该像这样使用它:

service.get(i).then(function(element) {
    // do something with element
});

另请注意,您定义承诺的方式是一种反模式。如果 http 请求失败,noticias 承诺永远不会被拒绝。使用承诺链:

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":"10" } })
      .then(function(response) {
          return response.data.responseData.feed.entries;
      });

我相信你想这样做:

app.factory('Noticias', function($http, $q) {
    var http_get_success_callback = function(data) {
        var entries = data.responseData.feed.entries;
        return entries;
    };

    var jsonData = $q.when($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" } }).then(http_get_success_callback));
    this.getAllNoticias = function() {
        return jsonData;
    }

    this.getIndexNoticia = function(index) {
        return jsonData[index];
    }

});

我的解决方案: 基于:

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