AngularJS待办事项列表应用程序
AngularJS ToDo List App
我在使用Angular JS创建todo列表应用程序时遇到了一些问题。我的HTML和Javascript代码如下:
HTML代码:
<!doctype html>
<html ng-app="todoApp">
<head>
<script src="bower_components/angular/angular.min.js"></script>
<script src="js/controller.js"></script>
<style>
.done-true {
text-decoration: line-through;
color: grey;
}
input:required {
box-shadow:none;
}
</style>
</head>
<body>
<h2>Todo</h2>
<div ng-controller="TodoListController">
<form ng-submit="addTodo()" name="form">
<input type="text" ng-model="todoText" size="30" placeholder="add new todo here" required>
<input class="btn-primary" type="submit" value="add">
</form>
<div>Incompleted : {{remaining()}}</div>
<div>Completed : {{todos.length - remaining()}}</div>
<a href="" ng-click="delete()">Delete</a>
<ul class="unstyled">
<li ng-repeat="todo in todos | orderBy : $index:true">
<button type="button" ng-click="remove()">✘</button>
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}" ng-hide="editing">{{todo.text}}</span>
<input type="text" ng-show="editing" ng-model="todo.text">
<button type="submit" ng-hide="editing" ng-click="change(todo); editing === true">Edit</button>
<button type="submit" ng-show="editing" ng-click="save(); editing === false">Save</button>
<button type="submit" ng-show="editing" ng-click="cancel(); editing === false">Cancel</button>
</li>
</ul>
<pre>NewField : {{newField | json}}</pre>
<pre>Todos : {{todos | json}}</pre>
</div>
</body>
</html>
我的Javascript代码:
var app = angular.module('todoApp', [])
app.controller('TodoListController', ['$scope', function($scope) {
$scope.todos = [];
$scope.newField = {};
$scope.addTodo = function() {
$scope.todos.push({text:$scope.todoText, done:false});
$scope.todoText = '';
};
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};
$scope.delete = function() {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function(todo) {
if (!todo.done) $scope.todos.push(todo);
});
};
$scope.remove = function(){
$scope.todos.splice(this.$index, 1);
};
$scope.editing = false;
$scope.change = function(field){
$scope.editing = $scope.todos.indexOf(field);
$scope.newField = angular.copy(field);
}
$scope.save = function(index) {
$scope.todos[$scope.editing] = $scope.todos;
$scope.editing = false;
};
$scope.cancel = function(index) {
$scope.todos[$scope.editing] = $scope.newField;
$scope.editing = false;
};
}]);
以下是我的问题:1.我的待办事项列表仍然是空的。当我添加一个列表并单击编辑时,它没有显示编辑表单、保存和取消按钮(它只复制到NewField)。然而,当我添加第二个列表并单击编辑时,它在2个列表上显示了编辑表单、保存和取消按钮(而不仅仅是第二个)。为什么?我的代码出了什么问题?
- 我的保存按钮不工作。当我更改编辑表单中的列表并单击保存时。该列表被清空并放在其他列表的底部。为什么
谢谢Billy
问题出现在$scope.editing
切换中。在更新了代码的一行之后,一切都开始正常工作。在$scope.change
功能中将$scope.editing = $scope.todos.indexOf(field)
更新为$scope.editing = true
;
var app = angular.module('todoApp', [])
app.controller('TodoListController', ['$scope', function($scope) {
$scope.todos = [];
$scope.newField = [];
$scope.addTodo = function() {
$scope.todos.push({text:$scope.todoText, done:false, editing: false});
$scope.todoText = '';
};
$scope.remaining = function() {
var count = 0;
angular.forEach($scope.todos, function(todo) {
count += todo.done ? 0 : 1;
});
return count;
};
$scope.delete = function() {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function(todo) {
if (!todo.done) $scope.todos.push(todo);
});
};
$scope.remove = function(){
$scope.todos.splice(this.$index, 1);
};
$scope.change = function(field){
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
}
$scope.save = function(index) {
$scope.todos[index].editing = false;
};
$scope.cancel = function(index) {
$scope.todos[index] = $scope.newField[index];
};
}]);
<!doctype html>
<html ng-app="todoApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<style>
.done-true {
text-decoration: line-through;
color: grey;
}
input:required {
box-shadow:none;
}
</style>
</head>
<body>
<h2>Todo</h2>
<div ng-controller="TodoListController">
<form ng-submit="addTodo()" name="form">
<input type="text" ng-model="todoText" size="30" placeholder="add new todo here" required>
<input class="btn-primary" type="submit" value="add">
</form>
<div>Incompleted : {{remaining()}}</div>
<div>Completed : {{todos.length - remaining()}}</div>
<a href="" ng-click="delete()">Delete</a>
<ul class="unstyled">
<li ng-repeat="todo in todos | orderBy : $index:true">
<button type="button" ng-click="remove()">✘</button>
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}" ng-hide="todo.editing">{{todo.text}}</span>
<input type="text" ng-show="todo.editing" ng-model="todo.text">
<button type="submit" ng-hide="todo.editing" ng-click="change(todo); todo.editing === true">Edit</button>
<button type="submit" ng-show="todo.editing" ng-click="save($index); todo.editing === false">Save</button>
<button type="submit" ng-show="todo.editing" ng-click="cancel($index); todo.editing === false">Cancel</button>
</li>
</ul>
<pre>NewField : {{newField | json}}</pre>
<pre>Todos : {{todos | json}}</pre>
</div>
</body>
</html>
相关文章:
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 谷歌应用程序脚本示例-如何更改单选按钮的列表框相关值
- 谷歌表单-根据分配给谷歌应用程序的工作为商业用户预先填充多项选择列表
- 使用谷歌应用程序脚本制作基于谷歌电子表格的带有列表框的网络应用程序
- Metro 应用程序中的列表视图项目单击
- 使用peta poco ORM在MVC应用程序中创建选择列表
- 访问可扩展QML应用程序中的列表视图索引
- 谷歌应用程序脚本web应用程序动态列表从表单
- JavaScript从应用程序获取SharePoint联机列表
- 应用程序脚本-自动从谷歌驱动器删除超过3天的文件-获取文件列表
- 选中的Angular HTML列表将所选值放入应用程序范围的变量和跨度中
- 如何在Sencha Touch应用程序的选择字段内自定义列表
- 主队/客队,在主干.js应用程序中提供球员列表
- 在待办事项列表应用程序中设置更新
- 使用JavaScript控制iOS网络应用程序中列表的垂直滚动和水平滑动
- AngularJS待办事项列表应用程序
- 可以't运行播放列表应用程序
- 来自codeccourse的Laravel待办事项列表应用程序
- 映射未定义的 ReactJS 代码中的待办事项列表应用程序
- 编辑和删除动态生成的列表项目在jQuery待办事项列表应用程序