af.database.object('item').set({ name: newName });
我可以在AngularFire2中使用此方法在已知節點中保存值。如何使用Angularfire2在Firebase中創建帶有自動編號的子項?
我想知道如何在每次這樣的獨特節點中保存。
在斯威夫特有這樣的事情:
node.childByAutoId().setValue([key: value]);
af.database.object('item').set({ name: newName });
我可以在AngularFire2中使用此方法在已知節點中保存值。如何使用Angularfire2在Firebase中創建帶有自動編號的子項?
我想知道如何在每次這樣的獨特節點中保存。
在斯威夫特有這樣的事情:
node.childByAutoId().setValue([key: value]);
原來這裏沒有一個班輪。 在AngularFire2中,推送方法僅適用於FirebaseListObservables,但不適用於FirebaseObjectObservable。所以這裏有一個用於演示的小組件:
import { Component } from '@angular/core';
import {AngularFire, FirebaseListObservable } from 'angularfire2';
@Component({
selector: 'auto',
template: `
<ul>
<li class="text" *ngFor="let p of person | async">
{{p.name}}
</li>
</ul>
<input type="text" #newname placeholder="Name" />
<br />
<button (click)="save(newname.value)">Set Name</button>
`
})
export class AutoComponent {
person: FirebaseListObservable<any>;
constructor(private af: AngularFire) {
this.person = af.database.list('person')
}
save(newName: string) {
this.person.push({ "name": newName });
}
}
你的意思是使用.push?
node.child('items').push({ key: value })
還是你的意思是你想用這個鍵保存一個節點?如果是這樣的話,請執行以下操作:
node.child('items').child(key).set(value);
這就是我們如何使用Firebase客戶端保存Javascript。我想知道如何使用angularfire2專門保存! :)和你的第一個代碼是如何存儲數據。在具有自動生成的ID的節點上。 –
您是否仍然可以在角度2中使用firebase.database()。ref()?我的建議不涉及angularfire2。 –
我不確定!由於我使用的是TypeScript,因此不知道如何在TypeScript中使用javascript庫的typeDefinitions。如果你能告訴我,那會很棒。 –