My goal is to create a dynamic list of items that can be edited individually. Each item should have its own editing mode.
Here's the code I'm using:
<ul ng-controller="ItemCtrl">
<li ng-repeat="item in items">
<div class="edit-off" ng-hide="item.editMode">...</div>
<div class="edit-on" ng-show="item.editMode">...</div>
<button ng-click="toggleEdit(item)">Edit</button>
</li>
</ul>
And here's the JavaScript part:
angular.module("app", [])
.controller("ItemCtrl", function($scope) {
$scope.items = [...]; // list of items
$scope.toggleEdit = function(item) {
item.editMode = !item.editMode;
};
});
After reviewing this code, I realized that I mistakenly attached the editMode
property to the controller scope instead of each individual item's scope. This caused all items to switch into editing mode simultaneously when any edit button was clicked.
I now understand that I need to assign an editMode
property to each item's scope so that they can be edited independently.