如何将对象绑定到数组中的firebase?

时间:2015-03-02 00:09:09

标签: angularjs firebase angularfire

当你有一个对象数组时,你如何将对象的更改绑定到firebase?

数据如下所示:

[{ title: "Do Something", Due: "1425121200000", assignedTo: "Bill", description: "Do something", $id: wqejkkjebiewdf},
 { title: "Do Something else", Due: "1425121200000",  assignedTo: "Jim", description: "Do something else", $id: owenuwefwfliu}]

JS:

var todos = this;
todos.tasks = [];
var todoRef = new Firebase(FB + "/tasks/" + CurrentFarm);
var todoArray = $firebase(todoRef).$asArray();
todoArray.$loaded(todos.tasks = todoArray);

html显示待办事项:

<div ng-repeat="task in todo.tasks | orderBy:'when'| filter: {done: true}">
<input type="checkbox" ng-model="task.done" ng-change="todo.taskDone(task)">
<span ng-repeat="users in task.users">{{getName(users)}} </span> 
<span> {{task.title}} </span>
<span> {{task.when | date:"EEEE, d MMMM, y"}} </span>
<input ng-click="editTask()" type="button"  value="Edit">
</div>

用于编辑待办事项的html。请注意,我从此代码中删除了某些输入,例如日期选择器等,以使其更具可读性。

<div  ng-show="editTaskMenu">
    <form  novalidate>
        <input ng-model="task.title" type="text" value="{{task.title}}">
        <textarea ng-model="task.description" value="{{task.description}}"></textarea>
        <input type="submit" value="Done" ng-click="finishedEditing()">
    </form>
</div>

更改以角度绑定到数组,但不会在firebase中更新。从我所知道的,没有办法用数组进行三向绑定,那么这里的工作是什么?

1 个答案:

答案 0 :(得分:1)

AngularFire完成三向绑定,这是您正在寻找的。

查看quick-start guide.


从快速入门指南:

将数据同步为对象:

var app = angular.module("sampleApp", ["firebase"]);
app.controller("SampleCtrl", function($scope, $firebaseObject) {
  var ref = new Firebase("https://<your-firebase>.firebaseio.com/data");
  // synchronize the object with a three-way data binding
  var syncObject = $firebaseObject(ref);
  syncObject.$bindTo($scope, "data");
});

来源:https://www.firebase.com/docs/web/libraries/angular/quickstart.html#section-objects

执行此操作时,syncObject将绑定到$scope.data,对$scope.data的任何更改都会更新syncObject,因此会更新firebase位置的数据。< / p>

但你想要的是$firebaseArray,如此:

var app = angular.module("sampleApp", ["firebase"]);
app.controller("SampleCtrl", function($scope, $firebaseArray) {
  var ref = new Firebase("https://<your-firebase>.firebaseio.com/messages");
  // create a synchronized array for use in our HTML code
  $scope.messages = $firebaseArray(ref);
});

来源:https://www.firebase.com/docs/web/libraries/angular/quickstart.html#section-arrays


以下是完整的AngularFire API documentation