简体   繁体   English

AngularFire 0.5.0 $ on'change'事件未正确触发

[英]AngularFire 0.5.0 $on 'change' event not firing correctly

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 我正在使用$on("change")事件来监听对indexlimit更改

.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. 我要注意的是-在初始加载时$on("change")事件会触发两次。

And every subsequent call to update the index or limit does not fire the $on("change") event. 并且随后每次更新indexlimit 调用都不会触发$on("change")事件。

<button type="button" ng-click="update();">Update</button>

Each time $scope.update fires, you assign the obj variable to a new reference. 每次触发$ scope.update时,您就将obj变量分配给一个新引用。 However, you only attach obj.$on(change) to the original obj . 但是,您只能将obj.$on(change)附加到原始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);
   });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM