I have simple app that inserts new record to Firebase and below the input field it simply lists last 10 items in database.
PROBLEM:
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();
}
}
SOLUTION #1 WITH NGZONE MANUAL UPDATE WHICH WORKED (thanks to: Günter Zöchbauer) Since the definition was outside the A2, the use of NgZone is really easy and corrected the problem. Thanks 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();
}
}
SOLUTION #2 AUTOMATIC A2 UPDATING WHICH WORKED (thanks to: Thierry) I simply put the definition inside the class of angular2, not outside, and everything started working as I assumed initialy. Thank you Thierry for the insight ;).
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();
}
}
constructor(private zone: NgZone)
...
zone.run(() => {
childSnapshot.forEach((records) => {
this.items.push(records.val());
});
});
...
See also Triggering Angular2 change detection manually
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With