I am adding an object in my array after a request. Everything goes well but I want the new datas to be displayed at the same time the response is received. When I receive the response, I push the new object in my array used to display messages in my view.
I tried to use ChangeDetectorRef
(detectChanges() method) and NgZone
(run() method) without success. Basically I want my view to go again on the ngFor to display the last message when sending it.
I searched on internet but couldn't find any answer for Angular except for one thread that didn't work.
Here's the part of my code where I want to trigger the rendering of the messages:
this.chatRequestService.send(this.navParams.get('id_chat'), nmMessage, this.token.value).subscribe(
(result) => {
this.messages.push(result);
},
(error) => {
console.log(error);
}
);
Here's my ngFor directive in the template:
<ng-container *ngFor="let message of messages[0]">
<ion-row>
<ion-col col-1 class="round-right"><ion-avatar item-start><img src="assets/imgs/BK.png" alt="Burger King" title="Burger King"></ion-avatar></ion-col> <!-- remplacer par {{message[0]?.sender.profileImageUrl}} -->
<ion-col col-9>
<div *ngIf="currentUser == message?.sender.id else elseBlock1" class="talk-bubble round-left me">
<div class="talktext">
<h5>{{message?.sender.username}}</h5>
<p>{{message?.content}}</p>
</div>
</div>
<ng-template #elseBlock1>
<div class="talk-bubble round-left">
<div class="talktext">
<h5>{{message?.sender.username}}</h5>
<p>{{message?.content}}</p>
</div>
</div>
</ng-template>
</ion-col>
</ion-row>
</ng-container>
Thank in advance to anyone who will take the time to read/answer.