gpt4 book ai didi

javascript - Material Angular 无限滚动与 $http 请求

转载 作者:数据小太阳 更新时间:2023-10-29 05:05:30 26 4
gpt4 key购买 nike

我正在使用 Angular Materialmd-virtual-repeat 指令来实现无限滚动,我需要将其替换为 demo $http 请求的 $timeout 函数。但我无法找到正确的解决方案。在下面的代码中,无限滚动工作正常但不显示来自 http 请求的数据。问题是我不知道如何将 $http 结果绑定(bind)到 infiniteItems

Here是 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 返回索引而不是项目
  • 如果您检查了您推送的内容,您会在我的 plunkr 的第 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_;
}));
}
}
}
})
})();

关于javascript - Material Angular 无限滚动与 $http 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33759185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com