让我们说我有文章我试图通过分页集合。
我使用了 $上(变)
事件侦听修改首页
和限制
.controller('articlesCtrl',[$范围,$火力点,$ stateParams功能($范围,$火力点,$ stateParams){ VAR上限= 2; VAR指数= $ stateParams.id; VAR articlesRef =新的火力地堡(https://example.firebaseio.com/articles).startAt(NULL,指数).limit(限制); VAR OBJ = $火力(articlesRef); OBJ。在$(变,函数(){ VAR键= $ OBJ getIndex()。 索引=按键[keys.length-1]; 的console.log(OBJ); }); $ scope.update =功能(){ 极限=上限+ 2; OBJ = $火力(articlesRef.startAt(空,指数).limit(限制)); }}]);
什么我注意到 - 是的初始负载的的 $上(变)
事件触发两次
![赛博朋克 光明记忆 稳了,白色ATX3.0 鑫谷开元T1全塔机箱装机点评](/d/file/2023/09-17/7673aa7415188eacd3d038abcefbc889.png)
和每次后续调用的更新首页
或限制
不火的 $上(变)
事件。
<按钮式=按钮NG点击=更新();>更新和LT; /按钮>
解决方案
每个时间$ scope.update火灾,你分配 OBJ
变量,一个新的参考。但是,您只重视 OBJ。$上(变化)
原来的 OBJ
。
这很可能与一些实验进行优化;这里有一个快速的蛮力,让你开始:
VAR上限= 2;VAR指数= $ stateParams.id;VAR articlesRef =新的火力地堡(https://example.firebaseio.com/articles);$ scope.update =更新;更新(); //初始化功能更新(){ 极限=上限+ 2; VAR OBJ = $火力(articlesRef.startAt(空,指数).limit(限制)); OBJ。在$(装,函数(){ VAR键= $ OBJ getIndex()。 索引=按键[keys.length-1]; 的console.log(OBJ); });}
Let's say I have a collection of articles I am trying to paginate through.
I'm using the $on("change")
event to listen for changes to index
and limit
.controller('articlesCtrl', ["$scope", "$firebase", "$stateParams", function($scope, $firebase, $stateParams) {
var limit = 2;
var index = $stateParams.id;
var articlesRef = new Firebase("https://example.firebaseio.com/articles").startAt(null, index).limit(limit);
var obj = $firebase(articlesRef);
obj.$on("change", function() {
var keys = obj.$getIndex();
index = keys[keys.length-1];
console.log(obj);
});
$scope.update = function(){
limit = limit + 2;
obj = $firebase(articlesRef.startAt(null, index).limit(limit));
}
}]);
What I'm noticing - is on initial load the $on("change")
event fires twice.
And every subsequent call to update the index
or limit
does not fire the $on("change")
event.
<button type="button" ng-click="update();">Update</button>
解决方案
Each time $scope.update fires, you assign the obj
variable to a new reference. However, you only attach obj.$on(change)
to the original obj
.
This could probably be optimized with some experimentation; here's a quick brute force to get you started:
var limit = 2;
var index = $stateParams.id;
var articlesRef = new Firebase("https://example.firebaseio.com/articles");
$scope.update = update;
update(); // initialize
function update(){
limit = limit + 2;
var obj = $firebase(articlesRef.startAt(null, index).limit(limit));
obj.$on("loaded", function() {
var keys = obj.$getIndex();
index = keys[keys.length-1];
console.log(obj);
});
}
相关推荐
最新文章