Skip to content

Instantly share code, notes, and snippets.

@REPTILEHAUS
Created February 28, 2017 22:00
Show Gist options
  • Save REPTILEHAUS/e2f4f1e0c05ce9ab08fa07f7705d873a to your computer and use it in GitHub Desktop.
Save REPTILEHAUS/e2f4f1e0c05ce9ab08fa07f7705d873a to your computer and use it in GitHub Desktop.
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ChatService } from '../chat.service';
@Component({
selector: 'app-chat',
template: `
<p>
REPTILEHAUS NG2 CHAT DEMO
</p>
<div *ngFor="let message of messages">
{{message.text}}
</div>
<input [(ngModel)]="message" /><button (click)="sendMessage()">Send</button>
`,
})
export class ChatComponent implements OnInit, OnDestroy {
messages = [];
connection;
message;
constructor(private chatService:ChatService) {}
sendMessage(){
this.chatService.sendMessage(this.message);
this.message = '';
}
ngOnInit() {
this.connection = this.chatService.getMessages().subscribe(message => {
this.messages.push(message);
})
}
ngOnDestroy() {
this.connection.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment