angularjs采用了棱角分明插槽-IO不执行承诺延期 [英] angularjs not executing deferred promise using angular-socket-io
问题描述
解决后,<一个href=\"http://stackoverflow.com/questions/35006814/angularjs-socket-io-event-forwarding-with-deferred-promise\">this问题我现在有与在等待一个套接字连接上一个函数内的角停止了新的问题。
控制器:
.controller('myCtrl',函数($范围,插座){ VAR SENDMSG =功能(MSG){
socket.then(函数(MSG){
socket.emit('newMsg',味精);
});
} //简单的例子
VAR味精=妈妈你好!;
SENDMSG(MSG);});
问题是, socket.emit()
永远不会被解雇。如果我删除 socket.then()
我会得到一个错误 socket.emit不是一个函数
。我们不得不等待插座进行初始化(这是基于另一个控制器成功登录并通过插座
工厂广播)。这是一个鸡生蛋蛋的悖论?我怎么能火socket.emit在另一个函数?其他类似职能的工作,如:
socket.then(功能(插座){
socket.on('连接',函数(){
//做一点事。这工作。
});
});
这个问题似乎是,你有两个信息
参数。然而,第二个是插座
VAR SENDMSG =功能(MSG){
socket.then(功能(插座){//&LT; -
socket.emit('newMsg',味精);
});
}
After solving this issue I now have a new issue with angular halting inside a function that's waiting on a socket connection.
Controller:
.controller('myCtrl',function($scope, socket) {
var sendMsg = function(msg) {
socket.then(function(msg) {
socket.emit('newMsg', msg);
});
}
//simplified example
var msg = 'Hi Mom!';
sendMsg(msg);
});
The problem is that socket.emit()
never gets fired. If I remove socket.then()
I will get an error socket.emit is not a function
. We have to wait for socket to be initialized (which is based on successful login in another controller and broadcast through the socket
factory). Is this a chicken-egg paradox? How can I fire socket.emit in another function? Other similar functions work, such as:
socket.then(function(socket) {
socket.on('connect',function() {
//do something. This works.
});
});
The problem seems to be that you are having two msg
parameters. However, the second one is the socket:
var sendMsg = function(msg) {
socket.then(function(socket) { // <--
socket.emit('newMsg', msg);
});
}
这篇关于angularjs采用了棱角分明插槽-IO不执行承诺延期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!