TODO with AngularJS (localStorage)



examples/angular/todo3.html
<script src="angular.min.js"></script>
<script src="todo3.js"></script>
<div ng-app="todoApp" ng-controller="todoController">
    <form ng-submit="add()">
    <input ng-model="title"><button>Add</button>
    </form>
    <ul>
        <li ng-repeat="t in tasks track by $index">{{ t }}
             <button ng-click="delete()">x</button></li>
    </ul>
</div>

examples/angular/todo3.js
angular.module('todoApp', [])
    .controller('todoController', ['$scope', function($scope) {
        var load = function() {
            var raw_data = localStorage.getItem('todo');
            if (raw_data === null) {
                $scope.tasks = [];
            } else {
                $scope.tasks = JSON.parse(raw_data);
            }
        };
        var save = function() {
            localStorage.setItem('todo', JSON.stringify($scope.tasks));
        }

        $scope.add = function() {
            $scope.tasks.push($scope.title);
            save();
        }
        $scope.delete = function() {
            $scope.tasks.splice(this.$index, 1);
            save();
        }

        load();
    }]);