0

I try to update and display message from variable updated into EventSource function:

ngOnInit(): void {
     const EventSource: any = window['EventSource'];
     const fx = new EventSource('/weather/update/');
     fx.onmessage = function (evt) {
          this.newMessage  = evt.data;
          console.log(this.newMessage);
     }
}

in template:

<p>{{newMessage}}</p>

The received message always is empty, but in console i can see the new message.

1 Answer 1

1

You're loosing context this. Try using arrow function in order to preserve this:

               like this     
                  \/
fx.onmessage = evt => {
   this.newMessage  = evt.data;
   console.log(this.newMessage);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.