与angularfire相当的firebaseRef.push()是什么?

Dav*_*e E 9 firebase angularfire

在下面的非angularfire伪代码中,我希望firebase生成一个用于推送新数据的密钥.

var ref = Firebase(...);
var newref = ref.push({"blah":"blah"});
var autoKey = newref.name();
Run Code Online (Sandbox Code Playgroud)

我尝试通过angularfire与绑定模型做同样的事情,但它只是给我错误的对象没有push()方法,类似于这个问题.当数据类型是数组时,他让它工作.

如何获得我在常规Firebase(非angularFire)中使用对象自动键看到的漂亮行为?

Ana*_*ant 7

如果要使用Object并具有自动生成的键,请使用a上的add方法angularFireCollection.例如:

function ExampleController($scope, angularFireCollection) {
  var url = 'https://angularFireExample.firebaseio-demo.com/';
  $scope.examples = angularFireCollection(url);
  $scope.addExample = function(ex) {
    $scope.examples.add(ex);
  };
}
Run Code Online (Sandbox Code Playgroud)

  • 为了记录,最终在$ $ scope.examples`上面的每个项目都会获得额外的成员`$ ref`,`$ id`和`$ index`,因为它们是从Firebase中撤出的.请参阅[angularFire.js]中定义的`angularFireItem`类(https://github.com/firebase/angularFire/blob/gh-pages/angularFire.js). (3认同)