javascript - AngularJS: Add and Edit List Items Using Same Input -
i'm trying edit list items in same input field use adding items.
have at
http://jsbin.com/retadexu/188/edit
and
http://jsbin.com/retadexu/192/edit
the first example works, have assign temporary object's .name property list object:
$scope.currentitem.name = $scope.newtodo.name;
the second, assign whole object, doesn't work:
$scope.currentitem = $scope.newtodo;
so if had more "name" property, have assign values?
instead of keeping track of current object, keep track of index in array of item.
$scope.savetodo = function(todo) { if ($scope.editmode) { $scope.currentitem = $scope.newtodo; $scope.todos[$scope.currentitemindex] = $scope.newtodo; $scope.editmode = false; } else { $scope.todos.push($scope.newtodo); } $scope.newtodo = ""; }; $scope.edittodo = function(todo) { $scope.editmode = true; $scope.newtodo = angular.copy(todo); $scope.currentitemindex = $scope.todos.indexof(todo); };
i moved text of button markup rather controller (personal preference)
<button ng-click="savetodo(todo)">{{editmode ? 'edit' : 'add'}}</button>
see working in plnkr
Comments
Post a Comment