Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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 无法通过多个重新启动的响应生成数组_Angularjs_Restangular - Fatal编程技术网

Angularjs 无法通过多个重新启动的响应生成数组

Angularjs 无法通过多个重新启动的响应生成数组,angularjs,restangular,Angularjs,Restangular,我使用Angular,必须填充一个表,为此,我需要一个数组,该数组通过多次调用服务器来获取值 我有以下场景 角度控制器: var application={}; var data-[]; $scope.tableData=[]; Restangular.all("a").getList().then(function(arr1){ for(var i=0;i<arr1.length;i++) { application.app=arr1[i];

我使用Angular,必须填充一个表,为此,我需要一个数组,该数组通过多次调用服务器来获取值

我有以下场景

角度控制器:

  var application={};
  var data-[];
  $scope.tableData=[];
  Restangular.all("a").getList().then(function(arr1){
     for(var i=0;i<arr1.length;i++)
     {
     application.app=arr1[i];
        Restangular.one("b",arr1[i].id).get().then(function(obj1){
         application.obj1=obj1;
        });
        Restangular.one("c",arr1[i].id).get().then(function(obj2){
         application.obj2=obj2;
        });
     data.push(application);
     application={};
       if(i==arr1.length-1){
       $scope.tableData=data;
     }
    }  

  });
(当前结果)


Restangular
是异步的(所有
Restangular.one()
在传递给
的回调之前被保留下来。然后
)。这就是为什么使用承诺

即使可以将restanglar设置为sync,也不应该这样做,因为这会在请求数据之前阻止浏览器,这将是一种糟糕的用户体验

您应该尝试进入
Promise
,因为它们被设计成看起来像同步代码,但行为却是异步的

您可以尝试以下方法:

     var a = Restangular.all("a").getList().then(function(arr1){
          // Some modification of the backend data response
          return modifiedData; // Now this should be passed to each of those Restanngular.one() methods sequentially
     });
上述代码将返回由
返回的
承诺
。然后
调用,该调用可以按照以下概念链接:

    (new Promise(function( resolve, reject){
       $timeout(function() {
         resolve("some");
       });
     }))
    .then(function(data) {
      return data+' data';
     })
    .then(function(data) {
       return new Promise(function(resolve, reject) {
       $timeout(function() {
          resolve(data+' !!!!!!');
       });
     });
    })
    .then(function(data) {
       // this will have 'some data !!!!!!'
         console.log(data);
     });

在循环的每次迭代中都覆盖相同的属性。这就是为什么您只能看到每个data.push(应用程序)中的最后一个;应用程序={};在这一步中,我将整个对象推送到一个数组中,并清空它以供下一次迭代使用。但在此之前,您有一个循环,每次都将每个请求的响应分配给错误输入的完全相同的属性。这部分在循环中不会改变您仍然将值分配给完全相同的
应用程序的事实。obj1
在一个循环中多次。只有最后一个作业会存在,实际上我在问题中提到“这是因为重新启动响应的异步行为,但无法理解如何处理它。”因此,请根据您的答案解释一下如何处理这个示例?
     var a = Restangular.all("a").getList().then(function(arr1){
          // Some modification of the backend data response
          return modifiedData; // Now this should be passed to each of those Restanngular.one() methods sequentially
     });
    (new Promise(function( resolve, reject){
       $timeout(function() {
         resolve("some");
       });
     }))
    .then(function(data) {
      return data+' data';
     })
    .then(function(data) {
       return new Promise(function(resolve, reject) {
       $timeout(function() {
          resolve(data+' !!!!!!');
       });
     });
    })
    .then(function(data) {
       // this will have 'some data !!!!!!'
         console.log(data);
     });