Typerror:set不是angularjs

时间:2016-04-28 01:27:34

标签: javascript angularjs firebase angularfire firebase-realtime-database

我遇到了这个错误:

TypeError:fb.set不是函数,angular.js:13550

而我正在尝试将数据发送到我的firebase数据库。

我真的不明白我在这里缺少什么,我抬头看文件,但一切看起来都不错。

var app = angular.module('app', ['firebase']);

app.controller("mainController", ["$scope", "$firebaseArray",
  function($scope, $firebaseArray) {

      var ref = new Firebase("https://burning-torch-4263.firebaseio.com/days");  
      fb =  $firebaseArray(ref); 

    $scope.reset = function() {  
    fb.set({
      monday: {
        name: 'Monday',
        slots: {
          0900: {
            time: '9:00am',
            booked: false
          },
          0110: {
            time: '11:00am',
            booked: false
          }
        }
      },
      tuesday: {
        name: 'Tuesday',
        slots: {
          0900: {
            time: '9:00am',
            booked: false
          },
          0110: {
            time: '11:00am',
            booked: false
              }
            }
          }
        });
    };    

  }

]);

2 个答案:

答案 0 :(得分:1)

对于firebase同步数组,您需要使用.$add(newArrayItem)而不是set

答案 1 :(得分:1)

适合我:

<!DOCTYPE html>
<html ng-app='app'>
<head>
  <meta name="description" content="AngularFire set data">
  <script src="https://cdn.firebase.com/js/client/2.2.6/firebase.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <script src="https://cdn.firebase.com/libs/angularfire/1.1.1/angularfire.min.js"></script>
  <meta charset="utf-8">
  <title>AngularFire set data</title>
</head>
<body ng-controller='MainCtrl'>
  <ol>
    <li ng-repeat='(id, day) in days'>{{day.name}} - {{id}}
      <ul>
        <li ng-repeat='slot in day.slots'>{{slot.time}}</li>
      </ul>
    </li>
  </ol>
</body>
</html>

和JavaScript

var app = angular.module('app', ['firebase']);

app.constant('FB_URL', 'https://stackoverflow.firebaseio.com/36903668');
app.controller('MainCtrl', function(FB_URL, $scope, $firebaseArray, $firebaseObject) {

  var ref = new Firebase(FB_URL+"/days");  
  var fb =  $firebaseArray(ref); 
  $scope.days = fb;

  ref.set({
    monday: {
      name: 'Monday',
      slots: {
        0900: {
          time: '9:00am',
          booked: false
        },
        0110: {
          time: '11:00am',
          booked: false
        }
      }
    },
    tuesday: {
      name: 'Tuesday',
      slots: {
        0900: {
          time: '9:00am',
          booked: false
        },
        0110: {
          time: '11:00am',
          booked: false
        }
      }
    }
  });
});

链接到jsbin:http://jsbin.com/lofima/edit?html,js,output