我使用main.js,想用DB显示数据,$ scope,如何实现?
var SelectChat = angular.module('SelectChat',
['ngRoute']).config(['$routeProvider',function($routeProvider){
$routeProvider.when("/",{
templateUrl: 'index.html',
controller: 'NameInfo'
});
}]).factory('socket',['$rootScope',function ($rootScope) {
var port = 8080;
var socket = io.connect('http://localhost:' + port);
return {
on: function(eventName, callback) {
var args = callback.value;
socket.on(eventName,callback);
//console.log(callback);
},
emit: function(eventName,data){
socket.emit(eventName,data);
console.log(data);
}
}
}]);
SelectChat.controller('NameInfo',function($scope,socket,$rootScope){
socket.on('allMessage',function(date){
$scope.nameChat = date.rows[0].nameChat;
});
}
index.html
<html>
<head>
<meta charset="utf-8">
<title>chat</title>
<script src="javascripts/socket.io.js"></script>
<script src="javascripts/jquery.js" charset="utf-8"></script>
<script src="javascripts/angular.js"></script>
<script src="javascripts/angular-route.js"></script>
<script src="javascripts/main.js" charset="utf-8"></script>
<link href="stylesheets/main.css" rel="stylesheet">
<!-- Здесь можете подключить ваш main.css -->
</head>
<body >
<header >
<h1>Chat</h1>
</header>
<main>
<div ng-app="SelectChat" ng-controller="NameInfo">
<p>Привет</p>
<p>{{nameChat}}/</p>
<p><span ng-bind="nameChat">{{nameChat}}</span></p>
</div>
</main>
<footer>
<input type="text" name="text" size="100">
<button type="button" name="button" id="sendMsg">Отправить</button>
</footer>
</body>
</html>
但是客户端侧页面上的数据无法显示,请在互联网答案中找到
。 on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
我不了解此代码的逻辑工作,并且无法在我的文件main.js中更改此代码。也许代码是正确的,但是我不理解逻辑工作。