我对Firebase和angular2相当陌生。因此,我试图通过教程制作一个Firebase集成的angular2聊天应用程序。我跟踪本教程制作了聊天应用程序。问题是,除了简单的聊天之外,当有人写或收到新的聊天时,我想做一个祝酒词。但我似乎找不到可以听那个事件的代码。
知道我在哪能找到它吗?
角分量看起来像这样
import { Component } from '@angular/core';
import { AngularFire, AuthProviders, AuthMethods, FirebaseListObservable } from 'angularfire2';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
items: FirebaseListObservable<any>;
name: any;
msgVal: string = '';
constructor(public af: AngularFire) {
this.items = af.database.list('/messages', {
query: {
limitToLast: 5
}
});
this.af.auth.subscribe(auth => {
if(auth) {
this.name = auth;
}
});
}
login() {
this.af.auth.login({
provider: AuthProviders.Facebook,
method: AuthMethods.Popup,
});
}
chatSend(theirMessage: string) {
this.items.push({ message: theirMessage, name: this.name.facebook.displayName});
this.msgVal = '';
}
}这是html标记
<div class="row columns">
<button (click)="login()" *ngIf="!name">Login With Facebook</button>
<input type="text" id="message" *ngIf="name" placeholder="Chat here..." (keyup.enter)="chatSend($event.target.value)" [(ngModel)]="msgVal" />
<div class="chat-container" *ngFor="let item of items | async">
<a href="#">{{item.name}}</a>
<p>{{item.message}}</p>
</div>
</div>发布于 2017-03-22 00:20:25
reddit的一位绅士帮了我,让我检查一下FirebaseObservables,并监听对它的更改。
initializeApp(firebaseConfig);
database().ref().on('value', function(snapshot){
var x = snapshot.val()
console.log("This prints whenever there is a new message");
});当聊天线程中有任何新消息时,主模块中的这个片段就会触发。
希望它能帮到别人
https://stackoverflow.com/questions/42920974
复制相似问题