I’m encountering an unusual situation where my checkbox isn’t getting ticked using Angular (1.5). Even after checking in chrome Batarang, everything seems fine but when I click on my link, the checkbox remains unchecked!?
Within an ng-repeat loop, I have an array being displayed. Inside this loop, there’s a checkbox and a hyperlink that should control the checkbox.
Is there anyone who can figure out why the hyperlink is failing to check the checkbox?
Thank you in advance,
Markup
<tr ng-repeat="o in vm.myArray track by o.Id">
<td>
<!-- This works (changes the checkbox to checked)-->
<a href ng-click="o.Highlight=true">Just to test it (this works)</a>
<input type="checkbox" ng-model="o.Highlight" ng-click="vm.highlight(vm.myArray.Days, o.Name);" style="width: 15px" />
<!-- This fails (I'm expecting it to check the checkbox) -->
<a href ng-click="vm.highlight(vm.myArray.Days, o.Name)">
{{o.Name}}
</a>
</td>
</tr>
This function simply iterates through an array of objects that contain a "Highlight" property (boolean) which determines UI elements. The Highlight property should toggle the checkbox status.
Despite Chrome batarang showing the correct Highlight property setting, the checkbox doesn’t toggle as expected.
Highlight method in controller
(function() {
"use strict";
angular.module("bla").controller("homeCtrl", ["stuff", function (stuff) {
var vm = this;
vm.highlight = highlight;
// Iterate through array items and toggle item highlighting if fieldToHighlight matches the item.Name.
function highlight(collection, fieldToHighlight) {
for (var i = 0; i < collection.length; i++) {
var element = collection[i];
if (element.Items != null) {
for (var counter = 0; counter < element.Items.length; counter++) {
var item = element.Items[counter];
if (fieldToHighlight == item.Name) {
if (item.Highlight) {
item.Highlight = false;
}
else {
item.Highlight = true;
}
}
}
}
}
}
Update after comments
Angular plugin for Chrome
The Highlight property is initially set to false. When clicking the test hyperlink, the property correctly changes to true.
angular plugin for chrome showing property correct data type
Update 2 - shows chrome debugging is setting the value correctly
During debugging, the Highlight property behaves correctly, even though the UI is not updating.
code being debugged showing the highlight is working even though the ui is not updating
Update after comments (13th december 2016)
Comments within the code:
<tr ng-repeat="o in vm.myArray track by o.Id">
<td>
<!-- checkbox -->
{{o.Highlight}}
<!-- clicking this changes the echoed variable above immediately but doesn't affect the checkbox based on o.Highlight -->
<input type="checkbox" ng-click="vm.highlight(o);" ng-model="o.Highlight" />
<!-- name -->
<!-- clicking this toggles the correct behavior in the UI dependent on the o.Highlight property but doesn't change the checkbox state -->
<a href ng-click="vm.highlight(o)">{{o.Name}}</a>
</td>
</tr>