mardi 17 mars 2015

AngularJS checkbox ng-change issue with $event.target

I'm writing a simple AngularJS Controller that keeps track of the number of checkboxes checked. Trying to avoid $scope.$watch and instead use ng-change to increment/decrement the total count.


HTML:



<form ng-controller="MainCtrl">
<table>
<tr ng-repeat="item in data">
<td>
<input type="checkbox"
value="{{item.id}}"
ng-model="item.selected"
ng-change="updateTotal($event)"> &nbsp; {{item.name}}
</td>
</tr>
</table>
<p>
Total checked: {{totalSelected}}
</p>
</form>


Controller snippet



$scope.updateTotal = function($event) {

var checkbox = $event.target;

if (checkbox.checked) {
$scope.totalSelected++;
}
else {
$scope.totalSelected--;
}
}


I keep getting an error in the controller where I attempt to access $event.target:



TypeError: Cannot read property 'target' of undefined


I created a Plunk for recreating: http://ift.tt/1BRnKdu


If anyone has any ideas or suggestions I would be very grateful.


Thank you very much!





Aucun commentaire:

Enregistrer un commentaire