Last active
December 22, 2015 11:48
-
-
Save mike-ward/6467920 to your computer and use it in GitHub Desktop.
ngChat Exercise
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 lang="en"> | |
<head> | |
<title>ngChat</title> | |
<!-- <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.2.1/pure-min.css"> --> | |
<!-- <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css"> --> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> | |
<style type="text/css"> | |
body { margin: 2pc; } | |
</style> | |
</head> | |
<body> | |
<h1>ngChat</h1> | |
<form> | |
<!-- | |
Steps: | |
1. Attach the ngChatApp to the body | |
2. Attach the "chatController" to the form | |
3. Create a list using ng-repeat to display the list of chats ($scope.messages) | |
4. Add two input fields, one for the name and one for the text | |
5. Bind the input fields to your model ($scope.newMessage.User, $scope.newMessage.Text) | |
6. Add a send button that calls $scope.send | |
7. Add a filter field and use it in ng-repeat field to filter responses | |
--> | |
</form> | |
<script> | |
var app = angular.module("ngChatApp", []); | |
app.config(['$httpProvider', function ($httpProvider) { | |
$httpProvider.defaults.useXDomain = true; | |
delete $httpProvider.defaults.headers.common['X-Requested-With']; | |
}]); | |
app.factory("chatService", function ($http) { | |
return { | |
get: function () { return $http.post('http://ngchat.azurewebsites.net/GetChats'); }, | |
<!-- get return example: [{"User":"Anonymous Coward","Text":"AngularJS Rocks!"}] --> | |
send: function (user, text) { return $http.post('http://ngchat.azurewebsites.net/AddChat', { User: user, Text: text }); } | |
}; | |
}); | |
app.controller("chatController", function ($scope, chatService) { | |
$scope.newMessage = { user: "", text: "" }; | |
var getChats = function () { chatService.get() | |
.success(function (response) { $scope.messages = response; }); }; | |
$scope.send = function () { | |
chatService.send($scope.newMessage.user, $scope.newMessage.text) | |
.success(function () { | |
$scope.newMessage.text = ""; | |
getChats(); | |
}); | |
}; | |
getChats(); | |
setInterval(getChats, 5000); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment