我正在编写一个简单的AngularJS控制器来跟踪检查的复选框的数量.试图避免$scope.$watch使用ng-change增加/减少总计数.
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)"> {{item.name}}
</td>
</tr>
</table>
<p>
Total checked: {{totalSelected}}
</p>
</form>
Run Code Online (Sandbox Code Playgroud)
控制器片段
$scope.updateTotal = function($event) {
var checkbox = $event.target;
if (checkbox.checked) {
$scope.totalSelected++;
}
else {
$scope.totalSelected--;
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试访问的控制器中收到错误$event.target:
TypeError: Cannot read property 'target' of undefined
Run Code Online (Sandbox Code Playgroud)
我创建了一个用于重新创建的Plunk:http://plnkr.co/edit/qPzETejmMHHZCQ2sV2Sk?p = info
如果有人有任何想法或建议,我将非常感激.
非常感谢你!