在angular厂使用sockets

即时通讯尝试使用angular度服务,以便从我的节点js服务器传递到我的视图json对象。 我debuged服务function,看到它真的得到的对象,但当我返回对象数组即时获取$ scope.messages = undefined。

我的服务器代码:

var express = require("express"); var url = require("url"); var app = express(); // express.createServer(); var server = require('http').createServer(app); var io = require('socket.io').listen(server); server.listen(8080); app.use(express.static(__dirname + '/public')); var MongoClient = require('mongodb').MongoClient , format = require('util').format; var insertMsg, Collection; var socketMap = {}; MongoClient.connect('mongodb://127.0.0.1:27017/EladGuy', function(err, db) { if(err) throw err; Collection = db.collection('messages'); console.log("connected to DB"); }); app.get("/*", function(request, response) { var screen = url.parse(request.url).pathname; check = String(screen).split("="); check2 = String(screen).split("/"); if (check[0] != "/screen" && check2[1] != "html") { response.sendFile(__dirname + '/public/index.html'); } else { if (check[1]>0 && check[1]<4) { saveScreen = check[1]; response.sendFile(__dirname + '/public/oldIndex.html'); } else if (check2[1] == "html") { response.sendFile(__dirname + screen); } else { response.sendFile(__dirname + '/public/index.html'); } } }); io.sockets.on('connection', function(socket) { socket.on('sendId', function(stationId) { socketMap[parseInt(stationId)] = socket; }); socket.on('getMessages', function(unUsed) { res = []; Collection.find({"screensId":{'$eq': parseInt(saveScreen)}}).toArray(function(err, docs) { docs.forEach(function(doc) { res.push(doc); }); socket.emit('sendMessages', res); }); }); socket.on('getAllMessages',function(unUsed){ res = []; Collection.find().toArray(function(err, docs) { docs.forEach(function(doc) { res.push(doc); }); console.log("STAM CHECK:" + res[0].name); socket.emit('sendAllMessages', res); }); }); }); 

main.js代码(用于循环,服务和控制器):

 var myApp = angular.module('myApp', ['ngRoute']); myApp.config(function($routeProvider) { $routeProvider .when('/', { controller: 'HomeController', templateUrl: 'menu.html' }) .when('/stations', { controller: 'StationsController', templateUrl: 'messagesView.html' }) .when('/', { controller: 'HomeController', templateUrl: 'menu.html' }) .otherwise({redirectTo: '/'}); }); var customersService = function ($http, $q) { var addsFactory = {}; var socket = io.connect('http://localhost:8080'); addsFactory.getMessages = function () { socket.emit('getAllMessages',null); socket.on('sendAllMessages',function (result) { return result; }); }; return addsFactory; }; myApp.factory('customersService', ['$http', '$q', customersService]); myApp.controller('StationsController',function($scope,customersService){ $scope.messages = []; $scope.messages = customersService.getMessages(); }); myApp.controller('HomeController',function($scope){ $scope.message="Loaded Menu!!"; }); 

可以somone帮我知道我的问题是什么?

你有你的addsFactory.getMessages封装在variablescustomersService 。 然后在一个外部函数中,你试图用customersService.getMessages();来调用它customersService.getMessages(); 。 您定义的方式是customersService本身就是一个函数,在这个函数中有一个带有函数getMessagesaddsFactory对象。 你能看到你的函数调用不符合吗? 有各种方法可以弥补这一点。 你需要在外部范围内定义这个函数:

 function getMessages(socket) { socket.emit('getAllMessages',null); socket.on('sendAllMessages',function (result) { return result; }); }; 

这样你将getMessages函数暴露给你的控制器并给它一个socket参数。 然后你可以通过getMessages(io.connect('http://localhost:8080');来调用它,然后将返回的值传递给你想要的任何东西,你应该阅读一些angular度最好的实践,我推荐John Papa的风格指南 。