我试图使用角度服务,以便从我的节点js服务器传递到我的视图json对象。 我对服务函数进行了调查,发现它确实是在获取对象,但当我返回对象数组时,我正在使用$ 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!!";
});
可以帮助我知道我的问题是什么?
答案 0 :(得分:-1)
您已将n == 1
封装在变量addsFactory.getMessages
中。然后在外部函数中,您尝试使用customersService
调用它。您定义它的方式是customersService本身就是一个函数,并且其中有一个customersService.getMessages();
对象,其函数为addsFactory
。你能看到你的函数调用与那个不匹配吗?有各种各样的方法可以解决这个问题。您需要在外部范围中定义此函数:
getMessages
这样,您可以将function getMessages(socket) {
socket.emit('getAllMessages',null);
socket.on('sendAllMessages',function (result) {
return result;
});
};
函数公开给控制器并为其指定getMessages
个参数。然后你可以通过socket
调用它并将返回的值传递给你想要的任何值。您应该阅读一些有棱角的最佳实践。我推荐John Papa's style guide。