【问题标题】:AngularJS Firebase $add is not a functionAngularJS Firebase $add 不是函数
【发布时间】:2016-09-08 04:10:01
【问题描述】:

我尝试将新记录推送到我的 Firebase。但每次我收到这样的控制台错误:

$scope.contacts.$add is not a function

这是我的代码:

app.controller('contactsCtrl',['$scope','$firebaseObject',function($scope,$firebaseObject){

 var ref = new Firebase("https://<database_details>.firebaseio.com/contacts");

 $scope.contacts = $firebaseObject(ref)

 $scope.addContact = function(){

     $scope.contacts.$add({
        name: $scope.name,
        address: $scope.address,
        telephone: $scope.telephone,
        company: $scope.company,
        email: $scope.email
        }).then(function(ref){

        var id = ref.key();
        console.log('contact added with Id: ' + id);


    });

        }; 
  }]);

【问题讨论】:

    标签: angularjs firebase firebase-realtime-database angularfire


    【解决方案1】:

    您应该使用$firebaseArray 而不是$firebaseObject

    app.controller('contactsCtrl','$scope','$firebaseArray',function($scope,$firebaseArray){
     var ref = new Firebase("https://<database_details>.firebaseio.com/contacts");
     $scope.contacts = $firebaseArray(ref)
     $scope.addContact = function(){
         $scope.contacts.$add({
            name: $scope.name,
            address: $scope.address,
            telephone: $scope.telephone,
            company: $scope.company,
            email: $scope.email
            }).then(function(ref){
            var id = ref.key();
            console.log('contact added with Id: ' + id);
        });
       }; 
      }]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2021-02-01
      • 2022-01-14
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多