本文介绍了AngularJS Firebase $add 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的代码:

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


    });

        };
  }]);

推荐答案

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

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

这篇关于AngularJS Firebase $add 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:48