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

How to bind objects to firebase inside an array?

当您有一组对象时,如何将对象的更改绑定到 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>

更改绑定到 angular 中的数组,但不会在 firebase 中更新。据我所知,没有办法对数组进行三向绑定,那么这里有什么工作要做?

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 位置的数据.

但你想要的是 $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