angularjs2 websocket客户端

时间:2016-01-18 04:00:24

标签: angularjs websocket

我对angularjs2有疑问。  有一个websocket接收组件,但是当通过onmessage进行websockt triger时,我更新了socket中的数据,但是这个属性没有更新到视图中,组件代码如下:

import {Component} from "angular2/core";
import {WSSocketService} from "./wsSocket.service";

@Component({
   selector: "ws-recv-view",
   templateUrl: "app/ws-receive-view.component.html"
})
export class WsRecvViewComponent{
  private receiveMsg: string;
  private _wsSocket: WSSocketService;

  constructor(_wsSocket: WSSocketService){
    this._wsSocket = _wsSocket;
    this.receiveMsg = this._wsSocket.getRecvMsg();
  }

 showMsg(){
  console.log("update recevie  message handle");
  var ws = this._wsSocket.wsHandle();
  ws.onmessage = function(evt){
  console.log("on  WsRecvViewComponent recevie message: " + evt.data);
  this.receiveMsg = evt.data;
  }
 }
}

1 个答案:

答案 0 :(得分:0)

我修复了它,this不是这个组件。这样的代码:

showMsg(){ console.log("update recevie message handle"); var ws = this._wsSocket.wsHandle(); var that = this; ws.onmessage = function(evt){ console.log("on WsRecvViewComponent recevie message: " + evt.data); that.receiveMsg = evt.data; } }