Skip to content

Instantly share code, notes, and snippets.

@owenl131
Created May 13, 2015 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owenl131/dd434f399df6fb2cf9b7 to your computer and use it in GitHub Desktop.
Save owenl131/dd434f399df6fb2cf9b7 to your computer and use it in GitHub Desktop.
owenfirebasechatapplication
<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.2.1/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/1.0.0/angularfire.min.js"></script>
<link rel="stylesheet" href="/resources/tutorial/css/example.css"/>
</head>
<body ng-controller="MyController">
<!-- CHAT MARKUP -->
<div class="example-chat l-demo-container">
<header>Owen's Firebase Chat Demo</header>
<div class="example-chat-toolbar">
<label for="nameInput">Username:</label>
<input ng-model="name" type="text" id="nameInput" placeholder="enter a username...">
</div>
<ul id="example-messages" class="example-chat-messages">
<li ng-repeat="msg in messages">
<strong class="example-chat-username">{{ msg.from }}</strong>
{{ msg.body }}
</li>
</ul>
<footer>
<input ng-model="msg" ng-keydown="addMessage($event)" type="text" id="messageInput" placeholder="Type a message...">
</footer>
</div>
<script>
var myApp = angular.module("myApp", ["firebase"]);
myApp.controller("MyController", ["$scope", "$firebaseArray",
function($scope, $firebaseArray) {
//CREATE A FIREBASE REFERENCE
var ref = new Firebase("https://afkehi7ebad.firebaseio-demo.com/");
// GET MESSAGES AS AN ARRAY
$scope.messages = $firebaseArray(ref);
//ADD MESSAGE METHOD
$scope.addMessage = function(e) {
//LISTEN FOR RETURN KEY
if (e.keyCode === 13 && $scope.msg) {
//ALLOW CUSTOM OR ANONYMOUS USER NAMES
var name = $scope.name || "anonymous";
//ADD TO FIREBASE
$scope.messages.$add({
from: name,
body: $scope.msg
});
//RESET MESSAGE
$scope.msg = "";
}
}
}
]);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment