Skip to content

Instantly share code, notes, and snippets.

@dbrgn
Created January 6, 2016 12:51
Show Gist options
  • Select an option

  • Save dbrgn/6aab558f8cb17f1f3809 to your computer and use it in GitHub Desktop.

Select an option

Save dbrgn/6aab558f8cb17f1f3809 to your computer and use it in GitHub Desktop.
Angular Demo
var app = angular.module('demoApp', []);
app.factory('numberFactory', function() {
var number = 0;
return function() {
number += 1;
return number;
};
});
app.controller('todoCtrl', function(numberFactory) {
this.todoList = [
{number: numberFactory(), text: "Do the laundry"},
{number: numberFactory(), text: "Bring out the thrash"},
{number: numberFactory(), text: "Feed the cat"}
];
this.addTodo = function() {
this.todoList.push({
number: numberFactory(),
text: this.newTodo
});
this.newTodo = "";
};
this.removeTodo = function(number) {
this.todoList = this.todoList.filter(function(elm, index, array) {
return elm.number != number;
});
};
});
<html ng-app="demoApp">
<head>
<title>Angular Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="app.js"></script>
<style>
.italic {
font-style: italic;
}
</style>
</head>
<body>
<div ng-controller="todoCtrl as ctrl">
<h1>My TODO list</h1>
<ul>
<li ng-repeat="todo in ctrl.todoList">{{ todo.number }}: {{ todo.text }}
<a href="#" ng-click="ctrl.removeTodo(todo.number)">Remove</a></li>
<li class="italic" ng-show="ctrl.newTodo">{{ ctrl.newTodo }}</li>
</ul>
<input type="text" ng-model="ctrl.newTodo">
<button type="button" ng-click="ctrl.addTodo()">Add</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment