Azz*_*ude 0 javascript checkbox jquery angularjs
我想我想在Angular中做一些相对简单的事情,但由于某种原因,我无法完全绕过ngClick,ngModel,ngChange或ngChecked来解决它.
我有一大堆重复的复选框,当检查一个时,一个函数提醒一件事,当它未经检查时,该函数会发出不同的警告.这是一些伪代码:
HTML:
<div id="topic" ng-repeat="50 times">
<input type='checkbox' ng-model='???' ng-click='myFunc($index)' />
</div>
Run Code Online (Sandbox Code Playgroud)
脚本:
function myFunc(index) {
if (checkbox == checked) {
alert('checkbox ' + index + 'checked!');
}
else {
alert('checkbox' + index + 'unchecked!');
}
}
Run Code Online (Sandbox Code Playgroud)
所以问题是我无法弄清楚如何判断重复中的哪个复选框以及何时取消选中.任何想法?
你需要控制器的帮助..
我会做这样的事情:
初始化一个空的数组值$scope和一个用于更改的函数.
$scope.checkboxes = [];
$scope.alert = function(index, event){
alert("checkbox " + index + " is " + $scope.checkbox[index]);
}
Run Code Online (Sandbox Code Playgroud)
绑定ng-model到checkboxes[$index],然后就可以使用ng-change,因为您指定的典范.
<input type="checkbox"
ng-model="checkbox[$index]"
ng-change="alert($index)">
Run Code Online (Sandbox Code Playgroud)
看到这个plunker.
| 归档时间: |
|
| 查看次数: |
9650 次 |
| 最近记录: |