我在合并来自网络服务的数据时遇到问题,该服务发送表中所选行的 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 个 finalOperationsList
和 liste
数组
感谢您的帮助
最佳答案
当您合并两个数组时,其余调用的回调尚未执行。
因此,当设置 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/