我有一个表,它列出了一系列规则。当点击复选框时,我需要它们获取这个"真"值并发布到API端点。这已经设置好了,但我得到的是"associated_rule"是未定义的。
我已经尝试在控制器中设置$scope.associated_rule.selected = true;
,但这仍然没有定义变量,并在控制台中引发相同的错误。
这是我的HTML表单:
<form name="rules_form" method="post" ng-submit="attach()">
<table class="table table-striped table-hover" ng-model="associated_rules">
<thead>
<th>Rule Types:</th>
<th>Description:</th>
<th>Start Time:</th>
<th>End Time:</th>
<th>Apply Rule to Vehicle:</th>
</thead>
<tr ng-repeat="associated_rule in associated_rules">
<td>@{{ associated_rule.resource_ids.accounts }}</td>
<td>@{{ associated_rule.description }}</td>
<td>@{{ associated_rule.start_time }}</td>
<td>@{{ associated_rule.end_time }}</td>
<td><input type="checkbox" ng-model="associated_rule.selected" aria-label="rule"></td>
</tr>
</table>
<button class="btn btn-primary" ng-click="attach()">Attach</button>
</form>
我的控制器事件:
$scope.attach = function () {
$scope.associated_rule.selected = true;
var i;
for (i = 0; i < $scope.associated_rule.selected.length; i++) {
//need to create a loop where the true value is picked up and then I can send the data using a POST method. But I'm stuck on this.
}
console.log(the result of the event);
};
目前,我只想将结果保存到console.log中,这样我就可以看到事件正在创建循环并显示结果。之后,我应该没事了。
任何帮助都将不胜感激。
我已经解决了这个问题,将$scope.rule定义为空数组,并在默认情况下将$scope.rule.selected设置为"false"。
太棒了!第一步!但是当你点击一个复选框时,复选框都是选中的——我想这可能是一个让我背部疼痛的ng重复。
$scope.rule = [];
$scope.rule.selected = false;
那么,我如何确保只设置我选择的复选框,而不是一次设置所有复选框呢?
也修复了这个问题;因为以上只是使整个阵列被选择;因为我没有深入阵列。做到了:
ng-model="rules.selected[associated_rule.id]"
通过在已定义的数组中对规则进行建模,它将在测试时显示出来。Brill.:)
单击按钮时,您错误地更改了复选框的值:
$scope.associated_rule.selected = true;
这将给出当前值,选择或未选择
$log.log($scope.associated_rule.selected);