然后大部分时间在angularFire的$ bind promises上返回太早

时间:2014-01-09 07:39:16

标签: javascript angularjs firebase angularfire

我希望在数据从firebase返回后执行操作,然后使用$ bind返回的promise。但是,当使用$ bind创建一个promise时,“then”函数将在从firebase返回数据之前运行。

多个$绑定只会导致数据从firebase返回后第一个“then”运行,但只有在我没有绑定到对象时才会这样。

在数据可用后,在firebase中的对象上运行$ bind的“then”似乎永远不会返回。

 angular.module('myApp', ['firebase'])
  .controller('HomeCtrl', ['$scope', '$rootScope', '$firebase',
    function($scope, $rootScope, $firebase) {


  // Data 1
  // The first bind's then will fire correctly
  $scope.data1 = $firebase(new Firebase("https://bindTest.firebaseio.com/users/1/email"));
  $scope.data1.$bind($scope, "data1Remote").then(function() {
    console.log("data1Remote: then");
    console.log($scope.data1Remote);
    $scope.data1RemoteThen = angular.copy($scope.data1Remote);
  });
  console.log("data1Remote: not then");
  console.log($scope.data1Remote);


  // Data 2
  // The then function doesn't seem to work at all if it references an object
  $scope.data2 = $firebase(new Firebase("https://bindTest.firebaseio.com/users/1"));
  $scope.data2.$bind($scope, "data2Remote").then(function() {
    console.log("data2Remote: then");
    console.log($scope.data2Remote);
    $scope.data2RemoteThen = angular.copy($scope.data2Remote);
  });
  console.log("data1Remote: not then");
  console.log($scope.data1Remote);


  // Data 3
  // Since this section follows another bind the then doesn't work.
  $scope.data3 = $firebase(new Firebase("https://bindTest.firebaseio.com/syncedValue"));
  $scope.data3.$bind($scope, "data3Remote").then(function() {
    console.log("data3Remote: then");
    console.log($scope.data3Remote);
    $scope.data3RemoteThen = angular.copy($scope.data3Remote);
  });
  console.log("data2Remote: not then");
  console.log($scope.data1Remote);

}
  ]);

除非我遗漏了某些东西,否则“then”函数应该只在firebase将其数据绑定到范围后运行。我创建了一个说明此问题的Plunker。尝试以不同的组合注释掉不同的数据部分,您会注意到正确返回的“thens”也会发生变化。有没有其他人经历过/找到这个问题的解决方案?感谢。

http://plnkr.co/edit/9UgDxx?p=preview

1 个答案:

答案 0 :(得分:0)

事实证明这是一个错误。我报告了它和Anant超快速地修复它。真棒。该修复程序应用于angularFire github的主分支。

相关问题