angular for循环返回最后一个值
angular for loop returns last value
我相信这是一个非常简单的问题,我在论坛上找到了一些解决方案,但看起来我做得很好,但仍然只得到了最后一个值。我写了一个简单的示例。有人可以解释我,我的错误在哪里?
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'Reinforcement';
$http.get('test.json')
.success(function (data) {
$scope.test = data;
});
$scope.getName = function () {
for (var i = 0; i < $scope.test.length; i++) {
$scope.parent = $scope.test[i].name
}
}
});
html
<div class="dropdown category" style="position:relative">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
<ul class="dropdown-menu">
<li ng-repeat="objName in test" ng-click="getName()">
<a href=""> {{objName.name}}</a>
</li>
</ul>
</div>
{{ parent }}
使用ng-repeat
$index
获取名称。不要在你的模型上循环,看看它更简单:
<p>Hello {{name}}!</p>
<div class="dropdown category" style="position:relative">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
<ul class="dropdown-menu">
<li ng-repeat="objName in test" ng-click="getName($index)">
<a href=""> {{objName.name}}</a>
</li>
</ul>
</div>
{{ selectedCategory }}
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'Reinforcement';
$http.get('test.json')
.success(function (data) {
$scope.test = data;
});
$scope.getName = function (index) {
console.info($scope.test[index].name);
$scope.selectedCategory = $scope.test[index].name;
}
});
注意/建议:避免使用像parent
这样的模型名称,因为您可能会与$parent
混淆,后者提供对父作用域的访问权限。
在第二种情况下,您必须使用$parent.$index
获取父$index(因为嵌套了ng-ereaat),但这是相同的技巧=没有循环遍历数组模型:
html:
<ul class="dropdown-menu">
<li class="dropdown" ng-repeat="entity in entitis" >
<a class="trigger right-caret">{{ entity }}</a>
<ul ng-if="entity | filter : 'Main'" class="dropdown-menu sub-menu">
<li ng-repeat="domain in ast" ng-click="getDomain($parent.$index, $index)">
<a href=""> {{domain.name}}</a>
</li>
</ul>
</li>
</ul>
</div>
{{ items }}
<br/>
<br/>
<p><b>parent index : {{astParentIndex}}</b></p>
<p><b>index : {{astIndex}}</b></p>
js
$scope.getDomain = function (parentIndex, index) {
$scope.astParentIndex = parentIndex;
$scope.astIndex = index;
$scope.items = $scope.ast[parentIndex].children;
}
您需要将$index
传递到getName()
方法中。
您的HTML
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div class="dropdown category" style="position:relative">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
<ul class="dropdown-menu">
<li ng-repeat="objName in test track by $index" ng-click="getName($index)">
<a href=""> {{objName.name}}</a>
</li>
</ul>
</div>
{{ bindedVariable}}
</body>
您的控制器
然后使用所选对象的索引从$scope.test
数组中获得正确的值。
$scope.getName = function (index) {
$scope.bindedVariable= $scope.test[index].name
}
$scope.parent = $scope.test[i].name
这将每一次覆盖$scope.parent
,因此它只会是的最后一个值
您可以将ng重复项作为参数传递给getName
(但我建议您将其称为setName
或selectName
,这样更可读):
HTML
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<div class="dropdown category" style="position:relative">
<a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
<ul class="dropdown-menu">
<li ng-repeat="objName in test" ng-click="getName(objName.name)">
<a href=""> {{objName.name}}</a>
</li>
</ul>
</div>
{{ parent }}
</body>
</html>
JS
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $http) {
$scope.name = 'Reinforcement';
$http.get('test.json')
.success(function (data) {
$scope.test = data;
});
$scope.getName = function (name) {
$scope.parent = name;
}
});
Plunker
http://plnkr.co/edit/p3PFmQHFmllcXEvL7K0z
for
循环总是将$scope.parent
设置为最后一个元素。试试这个:
HTML
<li ng-repeat="obj in test" ng-click="getName(obj)">
<a href="">{{obj.name}}</a>
</li>
JS
$scope.getName = function(obj) {
$scope.parent = obj.name;
}
相关文章:
- Foreach循环只返回最后一个值
- 获取最后一个返回值
- async.js在使用瀑布方法时返回最后一个值
- 检测圆圈内的标记gmap3,只返回数据库中的最后一个标记
- IE使用类jquery返回相同的最后一个单选按钮值
- angular for循环返回最后一个值
- 从数组中移除最后一个项(而不是返回项)
- ajax 返回带有循环的图像长度,检查最后一个循环
- 我必须创建一个函数,该函数返回传入的数组的最后一个元素
- 获取多个元素:只返回最后一个
- 从 for 循环内的 AJAX 链中的最后一个 AJAX 请求返回值
- 使用 jQuery 创建动态元素 单击事件始终返回 for 循环中的最后一个索引
- 返回路径中的最后一个目录
- 为什么从其他网络返回会保留最后一个哈希值,同时显示第一个哈希值的内容
- jQuery Deferred只返回循环中的最后一个值
- 日期选取器必须在最后一个选定日期返回
- D3.js .rangeBands() 返回“undefined”,只有最后一个值
- 当移动到最后一个li时,返回到第一个li(使用Jquery滚动到下一个)
- 单选按钮选择总是返回最后一个值
- 使用Replace With返回数组的最后一个值