这是我的 main.js
var app = angular.module('app', []);
app.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true); // enable pushState
$routeProvider.when('/', {
templateUrl: '/app.html',
controller: 'AppCtrl'
});
$routeProvider.when('/another/route', {
templateUrl: '/deep.html',
controller: 'AppCtrl'
});
});
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
socket.on('sendCustomer', function (data) {
console.log(data);`
//here i want to set controller scope
});
app.controller('AppCtrl', function ($scope) {
$scope.model = {
message: 'This is my app!!!'
};
$scope.getCustomer = function(imageUrl) {
alert("here i have to send command to socket io ");
socket.emit('getCustomer', { });
};
});
这是 index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset='utf-8'>
<title>Egghead Videos</title>
<link rel='stylesheet' href='/vendor/foundation/foundation.min.css'>
</head>
<body>
<div ng-app='app'>
<ng-view></ng-view>
</div>
<script type='text/javascript' src='/vendor/angularjs/angular.js'></script>
<script type='text/javascript' src='/socket.js'></script>
<script type='text/javascript' src='/main.js'></script>
</body>
</html>
这是 app.html
<h1>{{model.message}}</h1>
<a href="" ng-click="getCustomer()">Get Customer Data</a>
<!-- here i want to do ng-repeat of customer data -->
这是我的服务器 .js
var express = require('express');
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
var fs = require('fs');
server.listen(8000);
app.use(express.static(__dirname));
app.use(app.router);
app.get('/', function (req, res) {
res.set('content-type', 'text/html');
res.send(fs.readFileSync(__dirname + '/index.html', 'utf8'));
});
app.get('/another/route', function (req, res) {
res.set('content-type', 'text/html');
res.send(fs.readFileSync(__dirname + '/index.html', 'utf8'));
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
socket.on('getCustomer', function (data) {
console.log(data);
socket.emit('sendCustomer', [{ name: 'c1' },{ name: 'c2' }]);
});
});
因此,当在 main.js 中触发客户端上的 sendCustomer 事件时,我想通过 ng-repeat 在控制器中显示该数据,如果可以,我可以这样做,那么如何..