如何使此循环工作?它's返回未定义的语言js
How do I get this loop to work? It's returning undefined angualrjs
我正试图从对象索引中位于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>
您的for循环是错误的,而且您正试图控制台记录在for循环中声明的变量,它将始终未定义!这是正确的循环示例:
for (i = 0; i < rules_id.length; i++) {
console.log(rules_id[i]);
}
请阅读一下for循环如何工作
但这里真正的问题是你的角度代码
如果你以一种更具声明性的方式重新思考,你想要的东西也许可以存档:
// 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>
在这种方法中,我们采用一些格式良好的输入并进行一些转换,以便更好地使用它
// 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>
相关文章:
- 这.SOMETHING 总是返回未定义的 - extjs
- React+Meteor:this.ops返回未定义
- 解析-为什么user.getSessionToken()返回未定义的结果
- Javascript函数返回未定义
- JavaScript-获取数据属性的值返回未定义的值
- .val()返回未定义的.text返回随机代码
- 为什么innerHTML返回“未定义”
- "这个“;正在返回未定义的
- 谷歌地图:函数返回未定义的值在console.log中运行良好
- Javascript json返回未定义
- importScripts在web工作程序中返回未定义的
- 访问嵌套 json 对象的属性将返回未定义
- NativeAppEventEmitter返回未定义的
- 我的函数返回“未定义”
- 新手Javascript函数返回未定义的石头剪刀游戏
- Nodejs模块函数返回未定义的,而不是异步的
- .getElementById在.createTextNode中返回未定义
- javascript函数不返回未定义的值
- Typeahead的Javascript类型返回未定义
- Javascript可见元素返回未定义