【发布时间】:2016-04-07 15:56:23
【问题描述】:
我正在尝试从对象索引中为 0 的对象中提取 ID。然后我需要将此密钥转换为字符串,以便能够将其传递到 api 端点。
我的控制器事件:
$scope.attach = function () {
var rules_id = $scope.rules.selected;
if (rules_id) {
var l = rules_id.length;
for (var i = 0, j = l; i < j;)
{
var key_value = rules_id[i];
}
}
console.log($scope.rules);
console.log(key_value);
console.log($scope.rules.selected);
console.log($scope.asset.id);
};
目前我只是尝试在控制台中定义每个变量,唯一返回未定义的是循环变量“key_value”。
我已经对照大量示例和循环标准设置对此进行了检查,但这是行不通的。
$scope.rules.selected 是一个复选框结果,其键由规则 ID 表示。见下文:
<form name="rules" method="post">
<table class="table table-striped table-hover" ng-model="associated_rules">
<thead>
<th>Rule ID:</th>
<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.id }}</td>
<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="rules.selected[associated_rule.id]"aria-label="associated_rule.id" ng-true-value="true"ng-false-value="false" value="rules.selected"></td></tr>
</table>
<button class="btn btn-primary" ng-click="attach()"><i class="fa fa-paperclip"></i> Attach</button>
</form>
【问题讨论】:
-
你能粘贴console.log($scope.rules);结果?
-
你应该使用
var rules_id = Object.keys($scope.rules.selected);吗?为什么for (var i = 0, j = l; i < j;)? -
ng-model不应用于您是 JavaScript 新手吗?只是想知道,因为您的 JS 代码中有很多错误。如果你是新手,我不建议以 Angular 为起点来学习。
标签: javascript angularjs loops