材料角度无限滚动与$http请求
Material angular infinite scroll with $http request
我正在使用Angular Material
的md-virtual-repeat
指令进行无限滚动,我需要用$http请求替换它的演示$timeout函数。但我无法找到正确的解决方案。在下面的代码中,无限滚动可以正常工作,但不会显示http请求中的数据。问题是我不知道将$http结果绑定到infiniteItems的方法。
这是plunker。
Index.html
<body ng-app="infiniteScrolling" class="virtualRepeatdemoInfiniteScroll">
<div ng-controller="AppCtrl as ctrl" ng-cloak>
<md-content layout="column">
<md-virtual-repeat-container id="vertical-container" flex>
<div md-virtual-repeat="item in ctrl.infiniteItems" md-on-demand
class="repeated-item" flex>
{{item.id}}
</div>
</md-virtual-repeat-container>
</md-content>
</div>
</body>
JS:
(function () {
'use strict';
angular
.module('infiniteScrolling', ['ngMaterial'])
.controller('AppCtrl', function ($timeout,$scope,$http) {
this.infiniteItems = {
numLoaded_: 0,
toLoad_: 0,
items:[],
getItemAtIndex: function (index) {
if (index > this.numLoaded_) {
this.fetchMoreItems_(index);
return null;
}
return index;
},
getLength: function () {
return this.numLoaded_ + 5;
},
fetchMoreItems_: function (index) {
if (this.toLoad_ < index) {
this.toLoad_ += 20;
$http.get('items.json').success(function (data) {
var items = data;
for (var i = 0; i < items.length; i++) {
this.items.push(items[i].data);
}
this.numLoaded_ = this.toLoad_;
}.bind(this));
}
}
};
});
})();
这个有效:
plnkr
- getItemAtIndex返回的是索引,而不是项
- 如果你检查一下你推的内容,你会在我的推文中的第33行看到,我插入了
obj.data
,而不是普通的obj
(function () {
'use strict';
angular.module('infiniteScrolling', ['ngMaterial'])
.controller('AppCtrl', function ($scope, $http) {
// In this example, we set up our model using a plain object.
// Using a class works too. All that matters is that we implement
// getItemAtIndex and getLength.
var vm = this;
vm.infiniteItems = {
numLoaded_: 0,
toLoad_: 0,
items: [],
// Required.
getItemAtIndex: function (index) {
if (index > this.numLoaded_) {
this.fetchMoreItems_(index);
return null;
}
return this.items[index];
},
// Required.
getLength: function () {
return this.numLoaded_ + 5;
},
fetchMoreItems_: function (index) {
if (this.toLoad_ < index) {
this.toLoad_ += 5;
$http.get('items.json').then(angular.bind(this, function (obj) {
this.items = this.items.concat(obj.data);
this.numLoaded_ = this.toLoad_;
}));
}
}
}
})
})();
在每次api调用中,都试图了解数据库是否有更多的记录。并在fetchMoreItems_函数中添加该条件。
fetchMoreItems_: function (index) {
if (this.toLoad_ < index && hasMoreRecords) {
this.toLoad_ += 5;
在我们的代码中,我们得到了类似的详细信息
- s当前页面:3
- sMore:true===>>这表示数据库在之后是否有更多记录获取逐页数据
- s总计页数:4
- s记录总数:36
来到这里,看到@alessandro bug的回答,这非常有帮助。我不得不稍微改变一下,所以我想把它分享给其他人来帮助我。我需要:
- 以避免在已经恢复数据的情况下(使用
this.hold
)收到滚动请求 - 从后端接收到全部数据时停止请求(使用
this.stop_
) - 在加载时隐藏内容以避免故障或空元素(再次使用
this.hold
)。在视图中,您需要在该元素上使用ng-hide
,因为ng-if
避免了该元素的存在,因此它不会第一次加载 - 实现刷新方法以在参数/过滤器从外部表单更改时重新加载数据
远非完美,但效果相当不错。
vm.elements = null;
vm.infiniteItems = { // Start of infinte logic
stop_: false,
hold: false,
numLoaded_: 0,
toLoad_: 0,
items: [],
refresh: function() {
this.stop_ = false;
this.hold = false;
this.numLoaded_ = 0;
this.toLoad_ = 0;
this.items = [];
},
getItemAtIndex: function (index) {
if (!this.hold) {
if (index > this.numLoaded_) {
this.fetchMoreItems_(index);
return null;
}
}
return this.items[index];
},
getLength: function () {
if (this.stop_) {
return this.items.length;
}
return this.numLoaded_ + 5;
},
fetchMoreItems_: function (index) {
if (this.toLoad_ < index) {
this.hold = true;
this.toLoad_ += 5;
var start = this.numLoaded_;
if (start > 0) start++;
MyService.getData(parameters)
.then(angular.bind(this, function (obj) {
if (obj && obj.elements > 0) {
vm.elements = obj.elements;
this.items = this.items.concat(obj.data);
if (obj.elements < this.toLoad_) {
this.stop_ = true;
}
this.numLoaded_ = this.items.length;
this.hold = false;
} else { // if no data
vm.elements = 0;
}
}));
}
}
} // End of infinte logic
注意:我的服务返回一个如下组成的对象:obj = {elements: INTEGER, data: ARRAY}
,其中元素告诉您完整查询的长度。
相关文章:
- Meteor如何接收HTTP请求
- 在我的情况下,如何进行http请求
- 使用插件收听Firefox标签的http请求
- http请求使用html而不是json进行响应
- 在node-js路由中发出http请求
- 是否可以在没有HTTP请求或<输入>
- Acync JS HTTP请求通知请求
- PhantomJs在控制台中发出http请求时不会返回任何状态
- PHP处理来自一个页面的http请求,并将其显示在另一个页面上
- Firefox插件-如何发出http请求
- 验收测试主要针对传出的http请求
- 验证HTTP请求来自NFC标签上的点击
- 如何制作'http请求'在Node js中同步
- 为什么我在Cloud9中的http请求总是返回错误
- AngularJS Typeahead和$http请求限制
- HTTP请求使用cURL,但在浏览器中使用javascript时会得到CORS响应
- 如何使用Chrome扩展API获取网页的所有HTTP请求
- 将http请求中的数据传递给C#控制器
- 在初始化路由之前发送$http请求
- 从客户端捕获 HTTP 请求(包括 ajax)