angularFire无法$向firebase添加数据

时间:2014-11-24 22:03:31

标签: javascript angularjs firebase angularfire

我正在使用angularFire并尝试使用$ add将表单中的数据保存到firebase。任何帮助将不胜感激。控制台记录所有工作,我能够在控制台中检索数据。对不起所有的代码...我想确保我提供了所需的所有材料。

app.js:

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

    creativeBillingApp.constant('FIREBASE_URI', "https://XXXX.firebaseIO.com/");

    creativeBillingApp.controller('MainCtrl', ['$scope', 'groupsService', function( $scope,  groupsService, $firebase ) {

    console.log('Works')


    $scope.newGroup = {
     name: '',
     status: ''

    };

   $scope.addGroup = function(newGroup){

   console.log(newGroup);

groupsService.addGroup();
  $scope.newGroup = {
    name: '',
    status: ''

  };
};
 $scope.updateGroup = function (id) {
   groupsService.updateGroup(id);
 };

 $scope.removeGroup = function(id) {
   groupsService.removeGroup(id);
 };
}]);




creativeBillingApp.factory('groupsService', ['$firebase', 'FIREBASE_URI',
  function ($firebase, FIREBASE_URI) {
    'use strict';
    var ref = new Firebase(FIREBASE_URI);
    return $firebase(ref).$asArray();

var groups = $firebase(ref).$asArray();

var getGroups = function(){
  return groups;
};

var addGroup = function (newGroup) {
  console.log(newGroup)
  groups.$add(newGroup);
};

var updateGroup = function (id){
  groups.$save(id);
};

var removeGroup = function (id) {
  groups.$remove(id);
};
return {
  getGroups: getGroups,
  addGroup: addGroup,
  updateGroup: updateGroup,
  removeGroup: removeGroup,
}

}]);

的index.html:

         <form role="form" ng-submit="addGroup(newGroup)">
            <div class="form-group">
              <label for="groupName">Group Name</label>
              <input type="text" class="form-control" id="groupName" ng-model="newGroup.name">
            </div>
            <div class="form-group">
              <label for="groupStatus">Group Status</label>
              <select class="form-control" ng-model="newGroup.status">
                <option value="inactive">Inactive</option>
                <option value="active">Active</option>
              </select>
            </div>
           <button type="submit" class="btn btn-default">Submit</button>
          </form>

这是我得到的错误:

TypeError: undefined is not a function
at Scope.$scope.addGroup (http://localhost:9000/scripts/app.js:35:19)

,app.js第35行引用groupsService.addGroup();来自上面给出的app.js代码。

1 个答案:

答案 0 :(得分:5)

首先,在创建$FirebaseArray后,您将返回自己的服务。您之后还要创建另一个$FirebaseArray

return $firebase(ref).$asArray();

删除该return语句。这导致您的服务提前返回,并且所有附加方法都不适用于您的服务。

groupService.addGroup()中,您正在调用push,这不是$asArray上的函数。您需要致电.$add()newGroup参数也未在控制器中传递。

$.push方法基于$firebase绑定。当您使用$FirebaseArray $add方法将新记录推送到Firebase时。

See the docs for more info

<强> Plunker Demo

var addGroup = function (newGroup) {
  console.log(newGroup)
  groups.$add(newGroup);
};

然后在您的控制器中,您只需致电:

$scope.addGroup = function(newGroup){
  groupsService.addGroup(newGroup);

  $scope.newGroup = {
    name: '',
    status: ''
  };
};