在“变化”事件AngularFire 0.5.0 $无法正常点火 [英] AngularFire 0.5.0 $on 'change' event not firing correctly

查看:104
本文介绍了在“变化”事件AngularFire 0.5.0 $无法正常点火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有文章我试图通过分页集合。

我使用了 $上(变)事件侦听修改首页限制

  .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(限制));
  }}]);

什么我注意到 - 是的初始负载的的 $上(变)事件触发两次

每次后续调用的更新首页限制不火的 $上(变)事件。

 <按钮式=按钮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);
   });
}

这篇关于在“变化”事件AngularFire 0.5.0 $无法正常点火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆