Home > Blockchain >  BehaviourSubject not updating html template Ionic 5
BehaviourSubject not updating html template Ionic 5

Time:09-17

I have a list of messages from a user to another and want to update them in real time in Ionic 5, Angular 11.

Im using BehaviourSubject and the last change is displayed on constructor. At ngOninit its not possible to look the changes (subscribe)

mesage.ts

  private categorySelectedSubject = new BehaviorSubject<any>();
  categorySelectedAction$ = this.categorySelectedSubject.asObservable();


   constructor(){
  this.categorySelectedAction$.subscribe(x => console.log("data here ", x))

  this.categorySelectedAction$ = this.chatService.getSpecificMessage(request)
}

     
  

  async sendMsg(savemessage) {
   this.categorySelectedSubject.next(savemessage)
  }
  <ion-list id="list" *ngIf="categorySelectedAction$  | async as products">
  
      <div  *ngFor="let chat of products['response']; let i = index; " >
      
      ..... ect ....

The result at subscritpion at constructor is only the last : savemessage and no previous data. The template on html is not changing at all.

Have tried also NgZone and ChangeDetectorRef but nothing work

CodePudding user response:

You have to use an Observable to get data and than combine with a BehaviourSubject to make an CRUD action.

Have a look at merge and combineLast operators.

  • Related