angular2 firebase实时更新无法正常工作
发布时间:2020-12-17 17:54:02 所属栏目:安全 来源:网络整理
导读:我有一个简单的应用程序,将新记录插入Firebase,在输入字段下方,它只列出数据库中的最后10个项目. 问题: 我需要开始输入某些输入字段或点击“更新”以获取Firebase中的数据以显示在我的列表中.看来,在处理完角度后数据已经完成,现在它已经初始化地显示了列表
我有一个简单的应用程序,将新记录插入Firebase,在输入字段下方,它只列出数据库中的最后10个项目.
问题: >我需要开始输入某些输入字段或点击“更新”以获取Firebase中的数据以显示在我的列表中.看来,在处理完角度后数据已经完成,现在它已经初始化地显示了列表.我尝试将“| async”添加到ngFor,但不起作用并导致控制台出错.一切都运行良好,只需要加载并显示数据onload,而无需在输入中输入击键或单击更新按钮. 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(); } } 解决方案#1与NGZONE手动更新工作(感谢:GünterZ?chbauer) 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',prevChildKey) => { zone.run(() => { childSnapshot.forEach((records) => { this.items.push(records.val()); }); }); }); } updateHello(r){ myFirebaseRef.push({ title: this.newmsg }); this.newmsg=""; r.focus(); } } 解决方案#2自动A2更新工作(感谢: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',prevChildKey) => { childSnapshot.forEach((records) => { this.items.push(records.val()); }); }); } updateHello(r){ this.myFirebaseRef.push({ title: this.newmsg }); this.newmsg=""; r.focus(); } } 解决方法constructor(private zone: NgZone) ... zone.run(() => { childSnapshot.forEach((records) => { this.items.push(records.val()); }); }); ... 另见Triggering Angular2 change detection manually (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |