Created
November 25, 2014 14:06
-
-
Save anonymous/5584f54261574818e687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html ng-app="chatApp"> | |
<head> | |
<title>NodeJS chat application</title> | |
<link rel="stylesheet" type="text/css" href="style.css"> | |
<script type="text/javascript" src="/socket.io/socket.io.js"></script> | |
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.js"></script> | |
<script type="text/javascript" src="js/app.js"></script> | |
</head> | |
<body ng-controller="chatCtrl"> | |
<div id="main"> | |
<p ng-repeat="m in messages"> | |
<span>{{ m.author }} : </span> | |
<span>{{ m.text }}</span> | |
</p> | |
<form> | |
<input ng-model="message" autocomplete="off" /> | |
<div><a class="btn" ng-click="sendMessage()">Send message</a></div> | |
</form> | |
</div> | |
</body> | |
</html> | |
AND JS | |
var chatApp = angular.module('chatApp',[]); | |
chatApp.factory('socket', [function(){ | |
var socket = io.connect(); | |
return socket; | |
}]); | |
chatApp.controller('chatCtrl', ['$scope', 'socket',function($scope,socket){ | |
$scope.messages = [ | |
{author: 'Author1', text: 'Some text'}, | |
{author: 'Author2', text: 'Another text'}, | |
{author: 'Author3', text: 'And the last text'}, | |
]; | |
socket.on('join', function(data){ | |
console.log('Somebody Joined'); | |
}); | |
$scope.sendMessage = function(){ | |
socket.emit('message', $scope.message); | |
$scope.messages.push({ | |
author: 'SOmebody', | |
text: $scope.message | |
}); | |
console.log($scope.message); | |
$scope.message = ''; | |
}; | |
socket.on('message', function(data){ | |
console.log(data); | |
$scope.messages.push({ | |
author: 'Somebody', | |
text: data | |
}); | |
}); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment