I'm implementing a feature where I have two views in my HTML5 code that display the same list.
<div class="list" data-ng-repeat="item in model.items">
<div class=list-item">
{{ item.name }}
<a data-ng-click="addToList(item)">
<span data-ng-hide="item.checked">Add</span>
<span data-ng-show="item.checked">Remove</span>
</a>
</div>
</div>
<div class="checkbox-list" data-ng-repeat="item in model.items">
<div class=list-item">
<input type="checkbox" data-ng-checked="item.checked" data-ng-click="addToList(item)" />
{{ item.name }}
</div>
</div>
In my controller, I've defined the method in the scope:
function addToList(item) {
item.checked = !item.checked;
for (var i = 0; i < this.$scope.items.length; i++) {
if (this.$scope.items.id == item.id) {
this.$scope.items[i] = item;
break;
}
}
}
When I click the Add
or Remove
button, the list updates but the checkbox remains unchecked.
Conversely, when I check a box, the list does not update accordingly.
How can I resolve this issue?