Angular EventSource动态绑定变量

Angular EventSource动态绑定变量,angular,Angular,我尝试更新并显示从变量更新到EventSource函数的消息: ngOnInit():void{ const EventSource:any=window['EventSource']; const fx=新事件源('/weather/update/'); fx.onmessage=函数(evt){ this.newMessage=evt.data; console.log(this.newMessage); } } 在模板中: {{newMessage} 收到的消息总是空的,但在控制台中我

我尝试更新并显示从变量更新到EventSource函数的消息:

ngOnInit():void{
const EventSource:any=window['EventSource'];
const fx=新事件源('/weather/update/');
fx.onmessage=函数(evt){
this.newMessage=evt.data;
console.log(this.newMessage);
}
}
在模板中:

{{newMessage}


收到的消息总是空的,但在控制台中我可以看到新消息。

您正在失去上下文
。尝试使用箭头功能以保留此:

               like this     
                  \/
fx.onmessage = evt => {
   this.newMessage  = evt.data;
   console.log(this.newMessage);
}