Reputation: 3418
I have a sample todo app in angularjs. My list of todos is an array. but everytime only the last value of array item is getting added to li's.
I have replicated the issue on a jsbin.
http://jsbin.com/simafuzidi/3/edit
I want the list of todo item to have all the fields that are typed in text boxes and not just the last one. I know this is a closure problem but I am not sure how to solve this in the current circumstances.
thanks
$scope.addTodo = function() {
$scope.todos.push({text:$scope.todoText1,text:$scope.todoText2,text:$scope.todoText3,text:$scope.todoText4,done:false});
$scope.todoText1 = '';
$scope.todoText2 = '';
$scope.todoText3 = '';
$scope.todoText4 = '';
};
<form ng-submit="addTodo()">
<input type="text" ng-model="todoText1" size="30"
placeholder="add new todo here1"><br />
<input type="text" ng-model="todoText2" size="30"
placeholder="add new todo here2"><br />
<input type="text" ng-model="todoText3" size="30"
placeholder="add new todo here3"><br />
<input type="text" ng-model="todoText4" size="30"
placeholder="add new todo here4"><br />
<input class="btn-primary" type="submit" value="add">
</form>
Upvotes: 1
Views: 191
Reputation: 133403
You should have seen console error in JSBIN and go through Does JSON syntax allow duplicate keys in an object?
Line 8: Duplicate key 'text'.
Code
$scope.todos = [
{todoText1:'learn angular',
done:true
},
{
todoText1:'build an angular app', done:false
}];
$scope.addTodo = function() {
$scope.todos.push(
{
todoText1:$scope.todoText1,
todoText2:$scope.todoText2,
todoText3:$scope.todoText3,
todoText4:$scope.todoText4,
done:false
});
$scope.todoText1 = '';
$scope.todoText2 = '';
$scope.todoText3 = '';
$scope.todoText4 = '';
};
HTML
<ul class="unstyled">
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.todoText1}} {{todo.todoText2}} {{todo.todoText3}} {{todo.todoText4}}</span>
</li>
</ul>
Upvotes: 1
Reputation: 42669
I believe your push statement should look something like this.
$scope.todos.push({text:$scope.todoText1,done:false},{text:$scope.todoText2,done:false},{text:$scope.todoText3,done:false},{text:$scope.todoText4,done:false});
If you want to push all items.
Upvotes: 0