是否可以定位FirebaseObjectObservable的子级?
请从下面检查RcTestAppComponent.save()
(注释行)。
示例来自:https://github.com/angular/angularfire2/blob/master/docs/3-retrieving-data-as-lists.md
import { Component } from '@angular/core';
import { AngularFire, FirebaseObjectObservable } from 'angularfire2';
@Component({
moduleId: module.id,
selector: 'app',
template: `
<h1>{{ item | async | json }}</h1>
<input type="text" #newname placeholder="Name" />
<input type="text" #newsize placeholder="Size" />
<br />
<button (click)="save(newname.value)">Set Name</button>
<button (click)="update(newsize.value)">Update Size</button>
<button (click)="delete()">Delete</button>
`,
})
export class RcTestAppComponent {
item: FirebaseObjectObservable<any>;
constructor(af: AngularFire) {
this.item = af.database.object('/item');
}
save(newName: string) {
this.item.set({ name: newName });
/*
// can I do somethin like this:
let index = 2;
this.item.child(index).set({ name: newName });
// index is a dynamic number;
// when I use .child() on this.item, i receive a error:
// TypeError this.item.child is not a function
*/
}
update(newSize: string) {
this.item.update({ size: newSize });
}
delete() {
this.item.remove();
}
}
谢谢
答案 0 :(得分:2)
我设法找到了一个解决方案,以set
一个对象为动态路径,采用不同的方法。
export class RcTestAppComponent {
/*
item;
constructor(af: AngularFire) {
this.item = af;
}
*/
constructor(af: AngularFire) {
// cleaner TypeScript approach than the above commented lines :o fftopic
}
save(newName: string) {
let index = 2; // it's a dynamic number
this.af.database.object('/item/' + index).set({ name: newName };
}