angularjs2 websocket client



我对angularjs2有问题。 有一个 websocket 接收组件,但是当 websockt triger 通过 onmessage 时,我从套接字更新数据,但此属性没有更新到视图中,组件代码是这样的:

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;
  }
 }
}

我修复了它,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; } }

相关内容

  • 没有找到相关文章

最新更新