Insert comments into an IONIC app

Asked

Viewed 1,149 times

1

I am creating an application where underneath the content of the page I have the option of the user to make a comment. I’m using firebase to register and login and I’ve even been able to record the message in the database, but only she, needed to put the name of the user or username, and the date the comment was made.

My html is 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 is 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 = "";
   }
 }

2 answers

0

When you log in, you receive this user + token + other information in the success of the login call. What you probably have to do is save this user to localstorage, see: Ionic Storage or Ionic Native Torage Remembering which localstorage you have to save as string using JSON.parse(code) and the JSON.stringfy(code) In the insertion click action of a new comment, you take who this user is from localstorage and go up to firebase.

Remembering that there are other ways to do this

  • And how do I do that? It’s my first app and I don’t have much sense of Ionic

-1

HTML file Ex:

<!-- Você pode usar essas marcações em seus comentários no arquivo HTML -->

<!-- no início do comentário
--> logo após o fim do comentário

Obs: in Visual Studio Code (free) they are in green.

FILE . TS (TYPESCRIPT)

// Basta colocar duas / antes de iniciar o comentário. No editor sugerido ficará em verde também.
  • 2

    Alexandre, cool you want to help, but this does not answer the question. The author does not want to know how to put comments in the code, read the question again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.