Skip to content

Instantly share code, notes, and snippets.

@Hendrixer
Created July 31, 2015 21:36
Show Gist options
  • Save Hendrixer/e7cfae96285d52e3fb25 to your computer and use it in GitHub Desktop.
Save Hendrixer/e7cfae96285d52e3fb25 to your computer and use it in GitHub Desktop.
ng-teach
angular.module('app', [])
.controller('AppController', function($scope, Todos, Reddit) {
Reddit.getFrontPage()
.then(function(posts) {
$scope.redditPost = posts;
})
$scope.newTodo = '';
$scope.todos = Todos.getState();
$scope.createTodo = function() {
Todos.add($scope.newTodo);
};
$scope.remove = function(index) {
Todos.remove(index);
};
})
.factory('Reddit', function($http) {
var getFrontPage = function() {
return $http({
method: 'GET',
url: 'http://www.reddit.com/.json'
})
.then(function(resp) {
return resp.data.data.children;
});
};
return {
getFrontPage: getFrontPage
};
})
.factory('Todos', function() {
var todos = [];
var add = function(newTodo) {
todos.push(newTodo);
};
var remove = function(index) {
todos.splice(index, 1);
};
return {
add: add,
remove: remove,
getState: function() {
return todos;
}
};
});
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>todo</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body ng-controller="AppController">
<section>
<form ng-submit="createTodo()">
<input type="text" ng-model="newTodo">
<button>create todo</button>
</form>
<input placeholder="search" ng-model="search">
<ul>
<li ng-click="remove($index)"
ng-repeat="post in redditPost | filter:search">
{{ post.data.title }}
</li>
</ul>
</section>
<pre><code>{{ todos | json }}</code></pre>
<script src="lib/angular/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment