在angularJS中合并来自一个服务的两个数组
merging two arrays from a service in angularJS
我在合并来自web服务的数据时遇到了一个问题,该服务发送表中所选行的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
阵列感谢的帮助
合并两个数组时,rest调用的回调尚未执行。因此,当设置了liste
数据时,您应该在回调中合并这两个列表。您可以使用空数组或一些初始数据初始化$scope.listfinal
。视图将随之更新。
$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);
}
我从您的代码中看到的另一件事是,您的service
存在针对promise反模式的问题
在服务内部,最好是:
function _getListOperationsById(val){
//instead of create your own promise object, chaining the promise object returned by $http and return it
return $http({
method : "GET",
url : "MyURL/getById:"+val
}).then(function mySucces(response) {
return response.data;
}, function myError(response) {
return $q.reject('Erreur into url '+response);
});
};
如果您不需要将服务中的响应作为中间层处理,我建议直接返回结果:
function _getListOperationsById(val){
//instead of create your own promise object, chaining the promise object returned by $http and return it
return $http({
method : "GET",
url : "MyURL/getById:"+val
});
};
并且其他人已经给出了解决方案,您应该在返回的promise then()
函数中将它们合并在一起。
相关文章:
- 在一个服务中引用多个API调用(Angular)
- 在angularJS中合并来自一个服务的两个数组
- 如何在使用typescript的另一个服务中的一个角度服务中使用$rootScope define
- 在Angular 2中的另一个服务中注入自定义服务
- 使用在另一个服务方法中声明为模块模式的服务方法之一
- 如何在使用sails JS执行res.view()函数调用之前调用另一个服务(函数)
- 当添加一个服务和一个过滤器或多个服务时,我在做什么导致未知提供者
- AngularJS:试图使用一个服务,得到错误"无法读取属性'那么'“未定义”;
- 将一个服务(工厂)注入到另一个服务中,这两个服务都是异步的
- 使用restangularpromise在angularJS中创建一个服务
- 在angular.js中重用一个服务中的方法
- 配置依赖于另一个服务angularjs的服务
- 常量在一个服务中未定义,但在另一个服务中有效
- 我需要使用一个服务来跨多个视图存储、保存和提供可用的数据
- 如何在另一个服务中使用一个服务
- 为另一个服务注入服务
- AngularStrap——从一个服务中调用一个模态
- 如何从一个控制器设置一个服务的值,并使用该服务更新另一个控制器的值
- 用AngularJS传递一个变量给一个服务
- AngularJS - routeProvider解析调用一个服务方法