如下所示:
<Meta charset="UTF-8">
TodoList
<div class="todo" ng-controller="TodoListController">
<form ng-submit="addItem()">
<label for=""><a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>事项</label>
<input type="text" ng-model="todo">
</form>
<dl>
<dt>待办事项</dt>
<dd ng-repeat="todo in todos track by $index">
<input type="check<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>" ng-checked="todo.checked" ng-click="done($index,$event)">
{{todo.text}}
<a ng-href="" ng-click=" rel="external nofollow" rel="external nofollow" delete($index,todos)">删除</a>
</dd>
<dt>已办事项{{doneTodos.length}}</dt>
<dd ng-repeat="todo in doneTodos track by $index">
<input type="checkbox" ng-checked="todo.checked" ng-click="undone($index,doneTodos)">删除</a>
</dd>
</dl>
</div>
<script src="./libs/angular.min.js"></script>
<script>
// 定义一个模块
var App = angular.module('App',[]);
// 定义一个控制器
App.controller('TodoListController',['$scope',function($scope) {
// 待办事项
$scope.todos = [];
// 已完成事项
$scope.doneTodos = [];
// $scope.todo = '';
// 回车时<a href="https://www.jb51.cc/tag/diaoyong/" target="_blank" class="keywords">调用</a>ng-submit,往待办事项中<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>数据
$scope.addItem = function () {
// 向数组中<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>数据
$scope.todos.push({text:$scope.todo,checked: false});
// 清空输入框
$scope.todo = '';
}
// 勾选时完成
$scope.done = function (index,ev) {
// console.log(index);
// console.log($scope.todos);
// 从待办事项中<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>
var tmp = $scope.todos.splice(index,1);
tmp[0].checked = !tmp[0].checked;
// 将<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>的事项<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>到已完成里
$scope.doneTodos = $scope.doneTodos.concat(tmp);
ev.preventDefault();
}
// 取消已完成
$scope.undone = function (index,ev) {
// 从已完成数据中<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>
var tmp = $scope.doneTodos.splice(index,1);
tmp[0].checked = !tmp[0].checked;
// 将事项<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a>到待办事项中
$scope.todos = $scope.todos.concat(tmp);
// ev.preventDefault();
}
// <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>事项,传递当前索引和完整数据
$scope.delete = function (index,todos) {
// $scope.doneTodos.splice(index,1);
// console.log(todos);
// <a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a>索引值对应的事项
todos.splice(index,1);
}
}])
// var arr = [0,1,2,3,4];
// arr.splice(2,1)
</script>