2014-11-24 74 views
0

我正在使用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代碼。

回答

5

首先,您在創建$FirebaseArray後返回您的服務。之後你還會創建另一個$FirebaseArray

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

刪除該返回語句。這會導致您的服務提前返回,並且所有連接的方法都不適用於您的服務。

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: '' 
    }; 
}; 
+0

感謝您回覆!我已經嘗試了$ add,它在控制檯中給了我相同的錯誤。 – Lizajean 2014-11-24 23:19:53

+1

您正在從您的服務返回頂部。刪除該行。我已經更新了答案。 – 2014-11-24 23:22:52

+0

謝謝!然而,擺脫了錯誤,現在它在控制檯中返回未定義,當我從var addGroup下的console.log(newGroup)。 – Lizajean 2014-11-24 23:29:03