Created
February 28, 2017 22:00
-
-
Save REPTILEHAUS/e2f4f1e0c05ce9ab08fa07f7705d873a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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