Javascript 当返回的数据集有限时,如何递归地使用angular 1.6.5调用$http请求
我希望使用Angular 1.6.5进行项目重建,但我不确定在源每次仅返回有限数量的记录(每个请求返回1000条)并且需要获取2000多条记录时,如何在工厂中使用$http.get请求 在我当前的代码中,我使用jquery ajax,在.done方法中,我检查值“\uuu next”是否存在,如果存在,我调用传递值“\uu next”的函数。如果没有返回“\uuu next”值,我会对数据进行处理Javascript 当返回的数据集有限时,如何递归地使用angular 1.6.5调用$http请求,javascript,angularjs,angular-controller,angular-factory,Javascript,Angularjs,Angular Controller,Angular Factory,我希望使用Angular 1.6.5进行项目重建,但我不确定在源每次仅返回有限数量的记录(每个请求返回1000条)并且需要获取2000多条记录时,如何在工厂中使用$http.get请求 在我当前的代码中,我使用jquery ajax,在.done方法中,我检查值“\uuu next”是否存在,如果存在,我调用传递值“\uu next”的函数。如果没有返回“\uuu next”值,我会对数据进行处理 function getSpecifiedList(url){ var specUrl
function getSpecifiedList(url){
var specUrl = url;
$.ajax({
url: specUrl,
type: "GET",
headers:{"accept":"application/json;odata=verbose",
error: function(xhr){
console.log(xhr.status + " " + xhr.statusText);
}
}
}).done(function (results){
$("#wc_report_holder").text(results.length);
//buildObjects processes the results and adds to an array
buildObject(results);
if(results.d.__next){
getSpecifiedList(results.d.__next);
}else{
buildGridView();
}
}).fail(function(error){
$("#wc_report_holder").text("There was an error: " + error);
});
}
我想知道如何在angular 1.6.5中使用最佳实践和最有效的方法实现相同的值检查和递归调用,但我还没有在angular文档和谷歌搜索的基础上找到答案
下面是我目前使用Angular 1.6.5的一个简短版本
<script>
var sitesApp = angular.module("sitesApp", ['ngRoute']);
sitesApp.controller('SitesListCtrl', ['$scope', 'sites',
function ($scope, sites) {
sites.list().then(function (response) {
$scope.sites = response.data.value;
});
}
]);
sitesApp.controller("SiteDetailsCtrl", ['$scope', '$routeParams', 'sites',
function ($scope, $routeParams, sites) {
sites.find($routeParams.SiteCodePc, function (site) {
$scope.site = site;
});
}
]);
sitesApp.config(function ($routeProvider, $locationProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.
when('/', {
templateUrl: 'https://machine/sites/site-list.html',
controller: 'SitesListCtrl'
}).
when('/:SiteCodePc', {
templateUrl: 'https://machine/sites/site-details.html',
controller: 'SiteDetailsCtrl'
}).
otherwise({
redirectTo: '/'
});
});
sitesApp.factory('sites', ['$http', function ($http) {
var urlBase = "https://some-endpoint-for-data";
var cachedData;
function getData(callback) {
if (cachedData) {
callback(cachedData);
} else {
return $http({
method: 'GET',
url: urlBase
})
.then(function (response) {
//HERE IS WHERE I THINK THE SOLUTION NEEDS TO BE IMPLEMENTED
cachedData = response;
return cachedData;
});
}
}
return {
list: getData,
find: function (SiteCodePc, callback) {
getData(function (response) {
var site = response.data.value.filter(function (entry) {
//debugger;
return entry.SiteCodePc === SiteCodePc;
});
callback(site[0]);
});
}
};
}]);
</script>
<div ng-app="sitesApp">
<div ng-view></div>
</div>
var sitesApp=angular.module(“sitesApp”,['ngRoute']);
sitesApp.controller('SitesListCtrl',['$scope','sites',',
功能(范围、站点){
sites.list().then(函数(响应){
$scope.sites=response.data.value;
});
}
]);
控制器(“SiteDetailsCtrl”[“$scope”、“$routeParams”、“sites”,
功能($scope、$routeParams、sites){
sites.find($routeParams.SiteCodePc,函数(site){
$scope.site=站点;
});
}
]);
sitesApp.config(函数($routeProvider,$locationProvider){
$locationProvider.hashPrefix(“!”);
$routeProvider。
当(“/”{
templateUrl:'https://machine/sites/site-list.html',
控制器:“SitesListCtrl”
}).
当(“/:SiteCodePc”{
templateUrl:'https://machine/sites/site-details.html',
控制器:“SiteDetailsCtrl”
}).
否则({
重定向到:'/'
});
});
工厂('sites',['$http',函数($http){
var urlBase=”https://some-endpoint-for-data";
变量cachedData;
函数getData(回调){
如果(缓存数据){
回调(cachedData);
}否则{
返回$http({
方法:“GET”,
url:urlBase
})
.然后(功能(响应){
//我认为解决方案需要在这里实施
cachedData=响应;
返回缓存数据;
});
}
}
返回{
列表:getData,
查找:函数(SiteCodePc,回调){
getData(函数(响应){
var site=response.data.value.filter(函数(条目){
//调试器;
return entry.SiteCodePc==SiteCodePc;
});
回调(站点[0]);
});
}
};
}]);
提前感谢看起来您可以在接受第二个(可选)参数的情况下执行简单的递归。如果您是第一次调用getData(),那么您可以获得前1000个结果。但是,如果您找到_next,那么您将再次调用它发送当前的1000个结果,并将下一个1000个结果与前一个1000个结果合并
sitesApp.factory('sites', ['$http', function ($http) {
var urlBase = "https://some-endpoint-for-data";
function getData(callback, results) {
return $http({
method: 'GET',
url: urlBase
})
.then(function (response) {
// If you have found a previous batch of results then concat the two arrays
if(results) {
response = response.concat(results);
}
// If there are more results to be found then recursively call the same function passing the batched results
if(response.__next) {
return getData(callback, response);
}
// If there are no more results to be found then trigger your callback function
else {
callback(response);
}
});
}
return {
list: getData,
find: function (SiteCodePc, callback) {
getData(function (response) {
var site = response.data.value.filter(function (entry) {
//debugger;
return entry.SiteCodePc === SiteCodePc;
});
callback(site[0]);
});
}
};
}]);
我用分页逻辑和$q实现了相同的场景。在这个示例代码中,我根据LazyloadingLimit递归地提取记录。您可以根据自己的要求指定限制。因此,它仅根据总集合中的计数提取记录。在下面的示例中,我没有使用$http。在实际示例中,您可以使用$http从服务器中提取记录。在这里,我只是硬编码的集合最初 在您的情况下,您必须首先获取总记录数,然后应用一些分页逻辑或其他参数来提取下一个记录
angular.module('app', []);
angular.module('app').controller('SampleController', function ($scope,$http, $timeout, $q) {
// $scope.initialize();
$scope.mainCount = 0;
$scope.lazyloadingLimit = 2;
$scope.tileDefinitions = null;
$scope.tempList = null;
$scope.totalRecordCollection = [
{ "Name": "Record1" },
{ "Name": "Record2" },
{ "Name": "Record3" },
{ "Name": "Record4" },
{ "Name": "Record5" },
{ "Name": "Record6" },
{ "Name": "Record7" },
];
function getTotalRecordCollection() {
var deferred = $q.defer();
deferred.resolve($scope.totalRecordCollection);
return deferred.promise;
}
$scope.initialize = function () {
debugger;
var currentCount=0;
var pageList = new Array();
var currentPage = 1;
var numberPerPage = 2;
var numberOfPages = 0;
function makeList() {
numberOfPages = getNumberOfPages();
}
function getNumberOfPages() {
return Math.ceil($scope.tempList.length / numberPerPage);
}
function nextPage() {
currentPage += 1;
}
function loadList() {
var deferred = $q.defer();
if (currentCount !== $scope.tempList.length) {
var begin = ((currentPage - 1) * numberPerPage);
var end = begin + numberPerPage;
pageList = $scope.tempList.slice(begin, end);
currentCount = currentCount + pageList.length;
$scope.mainCount = currentCount;
deferred.resolve(true);
} else {
debugger;
return $q.reject();
}
return deferred.promise;
}
function loadNextRecords() {
loadList().then(function (res) {
nextPage();
loadNextRecords();
});
}
getTotalRecordCollection().then(function (response) {
debugger;
$scope.tempList = response;
makeList();
loadNextRecords();
});
}
});
<body ng-controller="SampleController">
<input type="button" value="Click Here" ng-click="initialize()"/>
{{mainCount}}
</body>
angular.module('app',[]);
角度.module('app').controller('SampleController',函数($scope,$http,$timeout,$q){
//$scope.initialize();
$scope.mainCount=0;
$scope.lazyloadingLimit=2;
$scope.tileDefinitions=null;
$scope.templast=null;
$scope.totalRecordCollection=[
{“名称”:“记录1”},
{“名称”:“记录2”},
{“名称”:“记录3”},
{“名称”:“记录4”},
{“名称”:“记录5”},
{“名称”:“记录6”},
{“名称”:“记录7”},
];
函数getTotalRecordCollection(){
var deferred=$q.deferred();
deferred.resolve($scope.totalRecordCollection);
回报。承诺;
}
$scope.initialize=函数(){
调试器;
var currentCount=0;
var pageList=新数组();
var currentPage=1;
var numberPerPage=2;
var numberOfPages=0;
函数makeList(){
numberOfPages=getNumberOfPages();
}
函数getNumberOfPages(){
返回Math.ceil($scope.templast.length/numberpage);
}
函数下一页(){
当前页面+=1;
}
函数loadList(){
var deferred=$q.deferred();
if(currentCount!=$scope.templast.length){
var begin=((当前页-1)*数字页);
var end=begin+numberpage;
pageList=$scope.templast.slice(开始、结束);
currentCount=currentCount+pageList.length;
$scope.mainCount=当前计数;
延迟。解决(正确);
}否则{
调试器;
返回$q.reject();
}
回报。承诺;
}
函数loadNextRecords(){
loadList()。然后(函数(res){
下一页();
loadNextRecords();
});
}
getTotalRecordCollection()。然后(函数(响应){
调试器;
$scope.tempLi