【问题标题】:What is the equivalent of firebaseRef.push() with angularfire?firebaseRef.push() 与 angularfire 的等价物是什么?
【发布时间】:2013-05-13 23:41:39
【问题描述】:

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

var ref = Firebase(...);
var newref = ref.push({"blah":"blah"});
var autoKey = newref.name();

我尝试通过 angularfire 使用绑定模型做同样的事情,但它只是给我关于对象没有push() 方法的错误,类似于this question。当数据类型是数组时,他就可以工作了。

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

【问题讨论】:

    标签: firebase angularfire


    【解决方案1】:

    如果您想使用对象并拥有自动生成的密钥,请在 angularFireCollection 上使用 add 方法。例如:

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

    【讨论】:

    • 啊,听起来很有希望,我今晚试试。除了看代码,还有 angularFire 的 API 参考吗?
    • 为了记录,每个以 $scope.examples 结尾的项目都会在从 Firebase 中拉出时添加额外的成员 $ref$id$index。请参阅angularFire.js 中定义的angularFireItem 类。
    • $scope.examples 会与 Firebase 数据同步吗?
    猜你喜欢
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2016-06-05
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多