AngularJS Firebase $add is not a function

泪湿孤枕 提交于 2020-01-24 00:15:27

问题


I try to push a new record to my Firebase. But everytime I receive a console error like this:

$scope.contacts.$add is not a function

Here is my code:

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);


    });

        }; 
  }]);

回答1:


You should use $firebaseArray instead of $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);
    });
   }; 
  }]);


来源:https://stackoverflow.com/questions/39382457/angularjs-firebase-add-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!