Created
May 13, 2015 03:24
-
-
Save daniellimws/dfdb05b51b1c8c60323f to your computer and use it in GitHub Desktop.
// source http://jsbin.com/diqejo
This file contains 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="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>Firebase Chat</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://jd1yqrmxlz5.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