我有一个FirebaseListObservable列表,我从firebase获取。我的对象看起来像这样:
{
"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"
}
}
这里我想按顺序更新Ids,使用forEach的FirebaseListObservable的一种方式。但随着名单的增长,这将太昂贵了。所以我的问题是, 1)如何在Ids中找到一些元素。 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*/
}
}
答案 0 :(得分:0)
你可以这样做:
import 'rxjs/add/operator/do';
this.todos = db.list('todos').do(data => this.length = data.length);
如果您想更新最后一项,请执行以下操作:
db.list('todos').update(this.length - 1, newData);