刚开始接触angularJS不太熟悉,我想做一个即时通讯的页面,想法是建立链接后,在onmessage里监听消息,有新消息的时候,能够在controller里有提示。代码如下,ws.onMessage(function(event) {
$rootScope.$apply(function(){
var message;
try {
message = JSON.parse(event.data);
} catch(e) {
console.info(e);
}
messages.push({
to : message.to,
from : message.from,
context : message.message,
sessionid : message.sessionId
});
});
});
```chatApp.controller("userListCtrl", ['$scope', '$http', 'msgService', function($rootScope, $scope, $http, msgService) {
//加载聊天用户列表
$rootScope.$on("messageListener", function(event, msgService) {
console.info("chage")
})
如上,第一次做angularJS,不太清楚,这个问题已经困扰我好几天了,网上资料也很少,就想有个人指点一下。