I'm creating an application in which under the content of the page I have the option of the user to make a comment. I am using the firebase to do registration and login and I even managed to save the message in the database, but only she needed to put the username or username and the date the comment was made.
My html looks like this:
<ion-content #content id="content">
<ion-card *ngFor="let message of messages">
<ion-card-header>
{{message.nome}}
</ion-card-header>
<ion-card-content>
{{message.mensagem}}
</ion-card-content>
<ion-footer>
<ion-toolbar>
<ion-input placeholder="Comente algo..." [(ngModel)]="message"></ion-input>
<ion-buttons end>
<button ion-button icon-right (click)="sendMessage()">
Enviar
<ion-icon name="send"></ion-icon>
</button>
</ion-buttons>
My ts looks like this:
export class Cap1SegObsPage {
@ViewChild("content") content: any;
username: string
message: string = ""
messages = [];
constructor(public navCtrl: NavController) {
this.getMessages();
}
getMessages(){
var messagesRef = firebase.database().ref().child("mensSegObsCap1");
messagesRef.on("value", (snap) => {
var data = snap.val();
this.messages = [];
for(var key in data){
this.messages.push(data[key]);
}
this.scrollToBottom();
});
}
scrollToBottom(){
var contentEnd = document.getElementById("content-end").offsetTop;
this.content.scrollTo(0, contentEnd, 300);
}
sendMessage(){
var messagesRef = firebase.database().ref().child("mensSegObsCap1");
messagesRef.push({mensagem: this.message, nome: this.username});
this.message = "";
}
}