监听收到的聊天事件
Listening for received chat event
我对 Firebase 和 angular2 都很陌生。所以我试图通过教程制作一个 Firebase 集成 angular2 Chat 应用程序。我已经按照 this tutorial 制作了聊天应用程序。问题是,除了简单的聊天之外,我还想在有人写信或收到新聊天时创建祝酒词。但我似乎找不到可以收听该事件的代码。
知道在哪里可以找到它吗?
angular 组件看起来像这样
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>
reddit 的一位绅士帮助了我,并告诉我检查 FirebaseObservables 并监听它的变化。
initializeApp(firebaseConfig);
database().ref().on('value', function(snapshot){
var x = snapshot.val()
console.log("This prints whenever there is a new message");
});
只要聊天线程中有任何新消息,主模块中的这个片段就会触发。
希望对大家有所帮助
我对 Firebase 和 angular2 都很陌生。所以我试图通过教程制作一个 Firebase 集成 angular2 Chat 应用程序。我已经按照 this tutorial 制作了聊天应用程序。问题是,除了简单的聊天之外,我还想在有人写信或收到新聊天时创建祝酒词。但我似乎找不到可以收听该事件的代码。
知道在哪里可以找到它吗?
angular 组件看起来像这样
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>
reddit 的一位绅士帮助了我,并告诉我检查 FirebaseObservables 并监听它的变化。
initializeApp(firebaseConfig);
database().ref().on('value', function(snapshot){
var x = snapshot.val()
console.log("This prints whenever there is a new message");
});
只要聊天线程中有任何新消息,主模块中的这个片段就会触发。
希望对大家有所帮助