我有一个简单的应用程序,将新记录插入Firebase,在输入字段下方,它只列出数据库中的最后10个项目.
问题:
import {Component,enableProdMode} from 'angular2/core'; enableProdMode();
import {FirebaseService} from 'firebase-angular2/core';
var myFirebaseRef = new Firebase("https://xxx.firebaseio.com/messages");
@Component({
selector: 'my-app',
template: `
<input type=text [(ngModel)]="newmsg" #newmsgref (keyup.enter)="updateHello(newmsgref)">
<button (click)="updateHello(newmsgref)">Update</button>
<ul>
<li *ngFor="#item of items">
{{item}}
</li>
</ul>
`
})
export class AppComponent {
newmsg:string;
items:Array<string>=[];
constructor() {
myFirebaseRef.limitToLast(10).on('child_added', (childSnapshot, prevChildKey) => {
childSnapshot.forEach((records) => {
this.items.push(records.val());
});
});
}
updateHello(r){
myFirebaseRef.push({
title: this.newmsg
});
this.newmsg="";
r.focus();
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案#1与NGZONE手动更新工作(感谢:GünterZöchbauer)由于定义超出了A2,因此使用NgZone非常简单并且可以解决问题.谢谢Günter;)
import {Component,enableProdMode,NgZone} from 'angular2/core'; enableProdMode();
import {FirebaseService} from 'firebase-angular2/core';
var myFirebaseRef = new Firebase("https://xxx.firebaseio.com/messages");
@Component({
selector: 'my-app',
template: `
<input type=text [(ngModel)]="newmsg" #newmsgref (keyup.enter)="updateHello(newmsgref)">
<button (click)="updateHello(newmsgref)">Update</button>
<ul>
<li *ngFor="#item of items">
{{item}}
</li>
</ul>
`
})
export class AppComponent {
newmsg:string;
items:Array<string>=[];
constructor(private zone: NgZone) {
myFirebaseRef.limitToLast(10).on('child_added', (childSnapshot, prevChildKey) => {
zone.run(() => {
childSnapshot.forEach((records) => {
this.items.push(records.val());
});
});
});
}
updateHello(r){
myFirebaseRef.push({
title: this.newmsg
});
this.newmsg="";
r.focus();
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案#2自动A2更新工作(感谢:Thierry)我只是将定义放在angular2的类中,而不是在外面,并且一切都按照我的假设开始工作.感谢Thierry的见解;).
import {Component,enableProdMode} from 'angular2/core'; enableProdMode();
import {FirebaseService} from 'firebase-angular2/core';
@Component({
selector: 'my-app',
template: `
<input type=text [(ngModel)]="newmsg" #newmsgref (keyup.enter)="updateHello(newmsgref)">
<button (click)="updateHello(newmsgref)">Update</button>
<ul>
<li *ngFor="#item of items">
{{item}}
</li>
</ul>
`
})
export class AppComponent {
newmsg:string;
items:Array<string>=[];
myFirebaseRef = new Firebase("https://xxx.firebaseio.com/messages");
constructor() {
this.myFirebaseRef.limitToLast(10).on('child_added', (childSnapshot, prevChildKey) => {
childSnapshot.forEach((records) => {
this.items.push(records.val());
});
});
}
updateHello(r){
this.myFirebaseRef.push({
title: this.newmsg
});
this.newmsg="";
r.focus();
}
}
Run Code Online (Sandbox Code Playgroud)
constructor(private zone: NgZone)
...
zone.run(() => {
childSnapshot.forEach((records) => {
this.items.push(records.val());
});
});
...
Run Code Online (Sandbox Code Playgroud)
另请参阅手动触发Angular2更改检测
Günter正是指出了问题;-)实际上,它myFirebaseRef是在Angular2处理之外创建的。
如果您对Firebase的自定义管道感兴趣,可以查看Angular Connect 2015上Sarah Robinson演讲的来源:
希望对您有帮助,蒂埃里
| 归档时间: |
|
| 查看次数: |
1860 次 |
| 最近记录: |