for循环只运行在angularjs
问题描述:
在js文件一次:for循环只运行在angularjs
$scope.isCheckboxChecked = function() {
var chks = document.getElementsByName('chk[]');
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
$scope.checkBoxClicked = true;
$('#btnUpdateData').attr("data-target", "#update-modal");
$('#btnUpdateData').attr("data-toggle", "modal");
break;
}
if ($scope.checkBoxClicked == false)
{
alert("Please select at least one checkbox");
}
}
}
在HTML
<div class="row" style="">
<button type="button" id="btnUpdateData" class="btn btn-info" ng-click="isCheckboxChecked()">Update</button>
</div>
此代码检查,如果该复选框被选中一次。 一旦复选框未选中,我希望for循环再次运行。
答
这里是你如何可以看一个复选框的statechange,做一些事情,当它改变Plunkr例如:
https://plnkr.co/edit/kyEv7IyAXGFRdZjaD4vM?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.$watch("testCheckBox", function(newVal, oldVal){
alert('Checkbox changed!')
});
});
HTML:
<body ng-controller="MainCtrl">
<p>Check box test</p>
<input type="checkbox" name="testCheckbox" ng-model="testCheckBox"></input>
</body>
缺少HTML一些代码? –
尝试没有休息; – DMCISSOKHO