angular - How to convert a Subject variable to an Observable variable -


let me explain. here html display messages this:

<message     *ngfor="let message of thread.messages | async"     [message]="message"> </message> 

my thread.messages variable observable.

export class thread {   id: string;   messages: observable<message[]> = new observable<message[]>(); } 

in addnewmessage() function, created subject variable add new messages in:

subjectmessages: subject<message[]> = new subject<message[]>();  addnewmessage(objmessage: any) : void {   const newmessage = new message(objmessage);   this.subjectmessages.next([newmessage]);   thread.messages = this.subjectmessages.asobservable(); } 

i want convert subjectmessages variable thread.messages variable. not work. thread.messages variable contains observable not display in html.

however, display of last message works :

thread.messages.subscribe((message: message[]) => {         [thread.lastmessage] = message; }); 

i despair, know mistake ? why can view last message, not messages?

in case, think of in subscriber state. in code emit single message wrapped in array, [newmessage]. subscribers these emitted values stream. if emit whole state, subscribers access whole array of messages.

it doesn't seem right use both subject , observable. haven't tried perhaps if instead:

export class thread {   id: string;   messages: subject<message[]> = new subject<message[]>(); }  messages: message[] = new message[];  addnewmessage(objmessage: any) : void {   const newmessage = new message(objmessage);   messages.push(newmessage);   thread.messages.next(messages); } 

or perhaps better approach @ replaysubject or behaviorsubject can used previous messages well.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -