我正在尝试从对象中索引处的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);
};
目前,我只是试图在控制台中定义每个变量,唯一返回undefined的是循环变量“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>
答案 0 :(得分:2)
你的for循环是错误的,你也试图控制日志在for循环中声明的变量,它总是未定义的! 这是正确的for循环示例:
for (i = 0; i < rules_id.length; i++) {
console.log(rules_id[i]);
}
请阅读for loop的工作原理
但真正的问题在于你的角色代码
答案 1 :(得分:0)
如果您以更具声明性的方式重新思考,您可能会存档的内容:
// creates the module
angular.module("samplemodule",[])
// apppend the controller to the module
angular.module("samplemodule").controller("SampleCtl",function(){
this.values = [// sample data
{a:"XXX",b:0,c:new Date(),d:true },
{a:"XXY",b:1,c:new Date(),d:false},
{a:"XYX",b:2,c:new Date(),d:false},
{a:"XYY",b:3,c:new Date(),d:true },
{a:"YXX",b:4,c:new Date(),d:false},
{a:"YXY",b:5,c:new Date(),d:false}
]
this.attach = function attach(){
var selectedones = this.values.filter(function(e){
return e.d // just the selected ones
}).map(function(e){
return e.b // just the ids or anything you want
}).join(",")
alert(selectedones)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="samplemodule" ng-controller="SampleCtl as ctl">
<table>
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="vl in ctl.values">
<td>{{vl.a}}</td>
<td>{{vl.b}}</td>
<td>{{vl.c}}</td>
<td><input type="checkbox" ng-model="vl.d"/></td>
</tr>
</tbody>
</table>
<button ng-click="ctl.attach()">The selected ones</button>
</div>
答案 2 :(得分:0)
在这种方法中,我们采取了一些格式良好的输入并进行了一些转换,以便我们可以更好地利用它
// creates the module
angular.module("samplemodule",[])
// apppend the controller to the module
angular.module("samplemodule").controller("SampleCtl",function(){
// log data from somewhere
this.originalData = [
"{db77370c-fbfe-11e5-8468-ed8c21fd14a1: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b5: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a2: false, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b6: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a3: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b7: true}",
"{db77370c-fbfe-11e5-8468-ed8c21fd14a4: true, c9f2c76c-fbfe-11e5-8fe5-70c120bc96b8: false}",
]
this.values = this.originalData.map(function(e){
e = e.replace(/{/g,"{\"").replace(/:/g,"\":").replace(/, /g,", \"")
e = JSON.parse(e)
var kv = []
for(var attr in e)
kv.push({key:attr,value:e[attr]})
return kv
})
this.attach = function attach(){
var selectedones = [].concat.apply([],this.values).filter(function(e){
return e.value
})
alert(selectedones)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="samplemodule" ng-controller="SampleCtl as ctl">
<table>
<tbody>
<tr ng-repeat="vl in ctl.values">
<td ng-repeat="kv in vl">
{{kv.key}}
<input type="checkbox" ng-model="kv.value"/>
</td>
</tr>
</tbody>
</table>
<button ng-click="ctl.attach()">The selected ones</button>
</div>