【发布时间】:2017-05-20 18:28:25
【问题描述】:
我有一个从 Firebase 获取的 FirebaseListObservable 列表。我的对象看起来像这样:
{
"0" : {
"completed" : false,
"description" : "Grab the grocerry",
"id" : 25,
"status" : true,
"title" : "Grocerry"
},
"1" : {
"completed" : false,
"description" : "some task",
"id" : 26,
"status" : true,
"title" : "Complete this"
}
}
这里我想按顺序更新 Id,一种使用 forEach 的 FirebaseListObservable 方式。但随着名单的增加,这将过于昂贵。所以我的问题是, 1)我怎样才能找到一些带有 ID 的元素。 2)如何获取ID的最后一个索引以便顺序更新ID。
我的代码:
import { Component, OnInit } from '@angular/core';
import { todo } from '../shared/todo';
import {AngularFireDatabaseModule,AngularFireDatabase, FirebaseListObservable} from 'angularfire2/database';
import {AngularFireAuthModule, AngularFireAuth } from 'angularfire2/auth';
@Component({
selector: 'app-add-task',
templateUrl: './add-task.component.html',
styleUrls: ['./add-task.component.css']
})
export class AddTaskComponent {
newTask :todo = new todo();
finalObject : todo = new todo();
active: boolean = true;
todos: FirebaseListObservable<any[]>;
constructor(afAuth: AngularFireAuth, db: AngularFireDatabase) {
// this.user = afAuth.authState;
this.todos = db.list('todos');
}
onSubmit() {
console.log(this.newTask);
console.log(this.todos);
this.finalObject = {
'id' : 29,
'title' : this.newTask.title,
'description' :this.newTask.description,
'status':true,
'completed' :false
};
//this.todos.push(this.newTask);
this.newTask = new todo();
/* To reset the form*/
this.active=false;
setTimeout ( ()=> this.active=true,0);
/* To reset the form*/
}
}
【问题讨论】:
标签: angular firebase firebase-realtime-database angular2-forms angularfire2