Javascript AngularFire类型错误:对象#<;对象>;没有方法';推动';

Javascript AngularFire类型错误:对象#<;对象>;没有方法';推动';,javascript,angularjs,firebase,angularfire,Javascript,Angularjs,Firebase,Angularfire,我正在尝试向我的firebase模型添加一个新对象。以下是我的Angularfire设置: angular.module('angularApp', ['firebase']) .controller('MyCtrl', ['$scope', 'angularFire', function MyCtrl($scope, angularFire) { $scope.shapes = []; var ref = new Firebase('https://angu

我正在尝试向我的firebase模型添加一个新对象。以下是我的Angularfire设置:

angular.module('angularApp', ['firebase'])
  .controller('MyCtrl', ['$scope', 'angularFire',
    function MyCtrl($scope, angularFire) {
      $scope.shapes = [];
      var ref = new Firebase('https://angular-starter.firebaseio.com/shapes');
      angularFire(ref, $scope, 'shapes');


      $scope.addShape = function () {
        console.log(typeof $scope.shapes);

        var newShape = {color: "#536993", height: 0, id: 1, left: 0, position: "absolute"};
         $scope.shapes.push(newShape);

      };
    }
  ])

我知道对象没有push方法,但我很难理解如何将模型定义为数组对象。我认为$scope.shapes=[];是否应该执行此任务?

看起来Firebase将$scope.shapes重置为对象。您可以使用对象方法,如

$scope.shapes[index]=新闻形状


存储数据而不是使用推送方法

使用angularFire将Firebase绑定到范围变量时,不能随意选择数据类型
angularFire
始终使用模型(对象),并将数据存储为对象

如果远程数据完全是数字键和顺序键,则可以通过使用
angularFireCollection
来模拟类似数组的行为(尽管Firebase始终将数据存储为对象,即使在这种情况下也是如此),它利用数组来存储值

请注意,即使在angularFireCollection的情况下,也可能不会调用
push()
,而是需要使用内置的
add()
方法

还要注意的是angularFire 0.5即将发布(可能下周发布),它将改善许多混淆

更新


AngularFire 0.5已发布,因此适当的方法现在是
$add
而不是
add
,并且AngularFire和angularFireCollection之间不再有区别。

看起来firebase必须用对象替换数组。据我所知,你的阵列def很好。我明白了,我会同时查看angularFireCollection并留意angularFire的更新。