【发布时间】:2016-09-02 10:32:55
【问题描述】:
app.controller('ctrl', function($scope){
var socket = io.connect();
this.messages = [];
this.sendMessage = function(){
socket.emit('new message', this.input_message);
this.input_message = '';
}
socket.on('new message', function(data){
this.messages.push(data);
});
});
我正在获取套接字发出的数据,但是当我尝试将该数据推送到 messages 数组时,它显示错误 cannot read the property push of undefined。我做错了什么?
【问题讨论】:
-
请提供fiddle或plunker
-
请告诉我数据是不是对象。在控制台打印数据
-
@SrinivasAppQube,它不是一个对象。
标签: javascript angularjs sockets scope socket.io