Reputation: 182
I have some problem when creating my todo list app using Angular JS. My HTML and Javascript code are the following :
HTML Code :
<!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>
My Javascript Code :
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;
};
}]);
Here are my problem : 1. My Todo list was still empty. When I added a list and clicked edit, it didn't show the edit form, save, and cancel button (It only copy to NewField). However, when I added a second list and clicked edit, it showed the edit form, save, and cancel button on the 2 list (not only the second list). Why ? What's wrong with my code ?
Thanks Billy
Upvotes: 2
Views: 1763
Reputation: 22041
The problem was in $scope.editing
toggling.
After updating one line of your code everything starts working well.
Updated $scope.editing = $scope.todos.indexOf(field)
to $scope.editing = true
within $scope.change
function;
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>
Upvotes: 2