Javascript 从angularJS中的服务合并两个阵列
我在合并来自web服务的数据时遇到问题,该web服务发送表中所选行的id,并使用此id获取数据,这是我在控制台中得到的:Javascript 从angularJS中的服务合并两个阵列,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我在合并来自web服务的数据时遇到问题,该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 = { getListOper
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);
}
合并2finalOperationsList
和liste
数组有什么帮助吗
感谢帮助合并两个数组时,rest调用的回调尚未执行。 因此,当设置了
liste
data时,您应该在回调中合并这两个列表。
您可以使用空数组或一些初始数据初始化$scope.listfail
。视图将随之更新
$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);
}
合并两个数组时,rest调用的回调尚未执行。 因此,当设置了
liste
data时,您应该在回调中合并这两个列表。
您可以使用空数组或一些初始数据初始化$scope.listfail
。视图将随之更新
$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);
}
我从您的代码中看到的另一件事是您的
服务
存在问题
在服务内部,最好是:
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()
函数中。我从您的代码中看到的另一件事是您的服务
存在问题
在服务内部,最好是:
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()
函数中将它们合并在一起。您正在异步加载$scope.list,因此$scope.list在使用concat时仍然未定义。您正在异步加载$scope.list,因此,在使用concat时,$scope.list仍然是未定义的。