Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何使用嵌套资源从不同URL连接JSON对象_Angularjs_Json_Angular Promise_Angular Http - Fatal编程技术网

Angularjs 如何使用嵌套资源从不同URL连接JSON对象

Angularjs 如何使用嵌套资源从不同URL连接JSON对象,angularjs,json,angular-promise,angular-http,Angularjs,Json,Angular Promise,Angular Http,我能把这些JSON对象合并成一个吗?原因是我有很多数据,因为我更愿意显示大量数据供用户过滤,而不是让他们在SPA中点击1000页,如果可能的话,我愿意加入他们(当然是以合理的方式) 编辑 我是这么想的 ///Returning JSON 1 $http.get("url1"). then(function (response) { $scope.foo = response.data; }); ///Returning JSON 2 $http.get("url2").

我能把这些JSON对象合并成一个吗?原因是我有很多数据,因为我更愿意显示大量数据供用户过滤,而不是让他们在SPA中点击1000页,如果可能的话,我愿意加入他们(当然是以合理的方式)

编辑

我是这么想的

///Returning JSON 1
$http.get("url1").
   then(function (response) {
       $scope.foo = response.data;
});

///Returning JSON 2
$http.get("url2").
   then(function (response) {
       $scope.foo = response.data;
});

///Returning JSON (n)
$http.get("n").
   then(function (response) {
       $scope.foo = response.data;
});
var url=”“;
对于(…i<100…){
url=”http://url.com“+i+”;
$http.get(url)。
然后(功能(响应){
$scope.foo.concat(response.data);
}
);
}
更新


我已经设法将JSON返回连接到一个对象数组中。但问题是这个数组现在包含的对象本身包含的对象本身包含的对象本身包含的对象数组。。。是的

如果它是数组,则可以对其进行concat

首先初始化空数组

var url ="";

for (... i < 100...) {
    url = "http://url.com"+i+"";
    $http.get(url).
        then(function(response){
           $scope.foo.concat(response.data);
           }
        );
}

您可以等待n个请求完成,然后对返回的对象执行任何操作

$scope.foo = [];

$http.get("url1").
   then(function (response) {
       $scope.foo.concat(response.data);
});
用于创建返回数组的承诺:

$q.all($http.get("url1"), $http.get("url2"), $http.get("url3"))
  .then(function (responses) { 
  // This function is called when the three requests return.
  // responses is an array with the first item being the result of
  // fetching url1, the second fetching url2, etc.

  // Depending on what the response looks like you may want to do
  // something like:

  $scope.data = angular.merge(responses[0], responses[1] /* etc */);
});
在层次结构的所有级别上使用
return
语句是很重要的:在
方法和函数本身中


连锁承诺 因为调用承诺的
.then
方法会返回一个新的派生承诺,所以很容易创建一个承诺链

可以创建任意长度的链,并且由于一个承诺可以用另一个承诺解决(这将进一步推迟其解决),因此可以在链中的任何点暂停/推迟承诺的解决。这使得实现强大的API成为可能


但是我有很多不同的URL需要循环,因为它们只有不同的页码。我会研究一下,谢谢。只要快速浏览一下有角度的文档,就永远不会发现这一点。
function arrayPromise(url, max)
    var urlArray = [];
    for (let i=0; i<max; i++) {
        urlArray.push(url + i);
    };
    var promiseArray = [];
    for (let i=0; i<urlArray.length; i++) {
        promiseArray.push($http.get(urlArray[i]);
    };
    return $q.all(promiseArray);
});
function nestedPromise (url, max) {
    var p1 = arrayPromise(url + "item/", max);

    var p2 = p1.then(function(itemArray) {
        var promises = [];
        for (let i=0; i<itemArray.length; i++) {
            var subUrl = url + "item/" + i + "/subItem/";
            promises[i] = arrayPromise(subUrl, itemArray[i].length);
        };
        return $q.all(promises);
    });
    return p2;
}; 
nestedPromise("https://example.com/", 10)
  .then(function (nestedArray) {
    $scope.data = nestedArray;
});