javascript - 合并来自 AngularJS 服务的两个数组

标签 javascript arrays angularjs

我在合并来自网络服务的数据时遇到问题,该服务发送表中所选行的 id,并使用此 id 获取数据,这就是我在控制台中得到的内容:

my all Liste [{ I get only the data of finalOperationsList not with the $scope.liste data}]

这是我的代码:

.factory('gammeMonatageFactory', function($http,$q){
     var backObject = { getListOperationsById:_getListOperationsById }
     return backObject;

     function _getListOperationsById(val){            
              var defer = $q.defer();
              $http({
                  method : "GET",
                  url : "MyURL/getById:"+val
              }).then(function mySucces(response) {
                  defer.resolve(response.data);
              }, function myError(response) {
                  deferred.reject('Erreur into url '+response);
              });  

              return defer.promise;
     };        
});

这就是我调用该服务的方式:

$scope.modifierOuCreerArticle = function() {    
     var v = $scope.OperationsList[$scope.OperationsList.length-1].Id;
     gammeMonatageFactory.getListOperationsById(v).then(function(Data){
         $scope.liste= JSON.parse(JSON.stringify(Data));  
         //I get the Data of $scope.liste only here I can't get this Data     outside this call                            
     });
     $scope.listfinal = $scope.finalOperationsList.concat($scope.liste);
     console.log("my all Liste "+$listfinal);
 }

请帮忙合并 2 个 finalOperationsListliste 数组 感谢您的帮助

最佳答案

当您合并两个数组时,其余调用的回调尚未执行。 因此,当设置 liste 数据时,您应该在回调中合并两个列表。 您可以使用空数组或一些初始数据来初始化 $scope.listfinal。 View 将随之更新。

$scope.modifierOuCreerArticle = function() {    
     var v = $scope.OperationsList[$scope.OperationsList.length-1].Id;
     gammeMonatageFactory.getListOperationsById(v).then(function(Data){
         $scope.liste = JSON.parse(JSON.stringify(Data));  
         $scope.listfinal = $scope.finalOperationsList.concat($scope.liste); // concat the arrays when $scope.liste is available                         
     });
     $scope.listfinal = $scope.finalOperationsList; // or initialize the list with empty array;
     console.log("my all Liste " + $listfinal);
 }

关于javascript - 合并来自 AngularJS 服务的两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36749515/

相关文章:

javascript - 从循环中删除重复项 - angularjs/javascript

javascript - Angular Testing : testing angular Services with private methods

javascript - 等待几个ajax调用完成,然后做一些事情

c - ‘unary *’ 的类型参数无效(有 ‘int’ )

javascript - Rails 4/jQuery : Need entry of value in a form input to autofill other inputs

arrays - swift数组的第n个元素的平均值

javascript - 如何将大数组切成固定长度的小块

angularjs - 如何在 ng-grid 内显示图像

javascript - 匹配中的变量(javascript)

javascript - 使用 jQuery 加载屏幕(或窗口)的页面全宽和高度?