单个元素上的多个 ng-repeat
Multiple ng-repeat on single element
是否可以实现这样的代码:-
<tr ng-repeat="data in dataArray,value in valueArray">
{{data}} {{value}}
</tr>
我有两个数组,我想在一行中显示它们。
PS:我不是要语法。我正在寻找实现这一目标的逻辑
谢谢
这是否符合您的需要
你的数据,值数组作为对象数组
this.obj = [{data: 'a', value :true}, {data: 'b', value:true}];
然后你像这样循环
<div ng:repeat="o in obj">
{{o.data}} and {{o.value}}
<input type="checkbox" ng:model="o.value">
</div>
Angular ng-repeat 不支持它,但您仍然可以根据需要编写自己的自定义指令。
更新部分
var traverseCollection = angular.module("CollectionTraverse", []);
traverseCollection.directive("repeatCollection", function(){
return {
restrict: "A",
scope: {
model: "="
},
controller: controller: function($scope) {
var collectionList = $scope.model;
angular.forEach(collectionList, function(obj, index) {
angular.forEach(obj, function(data, index) {
});
});
}
}
});
Your scope should contains the list of your collection objects : $scope.collectionList = [dataArray, valueArray];
Usage in HTML:
-------------------------------------------
<div repeat_collection model="collectionList"></div>
此指令将通用以遍历集合列表,是的,在上面的代码中可能存在一些语法错误,因为我没有 运行 它。是你的运气。
您应该在控制器中执行此操作,而不是在视图中。将 dataValues 映射到 key/value 对对象并使用索引引用值数组。这假设每个数据键都有一个对应的值键。
控制器:
var dataArray = [];
var valueArray = [];
this.repeatData = dataArray.map(function(value, index) {
return {
data: value,
value: valueArray[index]
}
});
查看:
<tr ng-repeat="data in repeatData">
{{data.data}} {{data.value}}
</tr>
如果出于某种原因,您有两个长度相同且内容对应的数组(array1[0]
对应于 array2[0]
,...,array1[n]
对应于array2[n]
), 你可以像这样使用 AngularJS 的 track by
(which was introduced for the 1st time in the version 1.1.4) 例如:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script>
<table ng-app="" ng-init="names=['Bill','Billa','Billy']; ages=['10', '20', '30']">
<tr ng-repeat="name in names track by $index">
<td>{{ name }} is {{ ages[$index] }} years old.</td>
</tr>
</table>
希望能帮到你。
如果您想要类似列表的内容,其中同一行包含两个或多个项目:
在 html 文件中:
<li ng-repeat="item in items">
<i class="material-icons">{{navItem[1]}}</i>{{navItem[0]}}</li>
在js文件中:
$scope.items = [
["Home", "home"],
["Favorite", "favorite"]
]
是否可以实现这样的代码:-
<tr ng-repeat="data in dataArray,value in valueArray">
{{data}} {{value}}
</tr>
我有两个数组,我想在一行中显示它们。
PS:我不是要语法。我正在寻找实现这一目标的逻辑
谢谢
这是否符合您的需要
你的数据,值数组作为对象数组
this.obj = [{data: 'a', value :true}, {data: 'b', value:true}];
然后你像这样循环
<div ng:repeat="o in obj">
{{o.data}} and {{o.value}}
<input type="checkbox" ng:model="o.value">
</div>
Angular ng-repeat 不支持它,但您仍然可以根据需要编写自己的自定义指令。
更新部分
var traverseCollection = angular.module("CollectionTraverse", []);
traverseCollection.directive("repeatCollection", function(){
return {
restrict: "A",
scope: {
model: "="
},
controller: controller: function($scope) {
var collectionList = $scope.model;
angular.forEach(collectionList, function(obj, index) {
angular.forEach(obj, function(data, index) {
});
});
}
}
});
Your scope should contains the list of your collection objects : $scope.collectionList = [dataArray, valueArray];
Usage in HTML:
-------------------------------------------
<div repeat_collection model="collectionList"></div>
此指令将通用以遍历集合列表,是的,在上面的代码中可能存在一些语法错误,因为我没有 运行 它。是你的运气。
您应该在控制器中执行此操作,而不是在视图中。将 dataValues 映射到 key/value 对对象并使用索引引用值数组。这假设每个数据键都有一个对应的值键。
控制器:
var dataArray = [];
var valueArray = [];
this.repeatData = dataArray.map(function(value, index) {
return {
data: value,
value: valueArray[index]
}
});
查看:
<tr ng-repeat="data in repeatData">
{{data.data}} {{data.value}}
</tr>
如果出于某种原因,您有两个长度相同且内容对应的数组(array1[0]
对应于 array2[0]
,...,array1[n]
对应于array2[n]
), 你可以像这样使用 AngularJS 的 track by
(which was introduced for the 1st time in the version 1.1.4) 例如:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/angular.min.js"></script>
<table ng-app="" ng-init="names=['Bill','Billa','Billy']; ages=['10', '20', '30']">
<tr ng-repeat="name in names track by $index">
<td>{{ name }} is {{ ages[$index] }} years old.</td>
</tr>
</table>
希望能帮到你。
如果您想要类似列表的内容,其中同一行包含两个或多个项目:
在 html 文件中:
<li ng-repeat="item in items">
<i class="material-icons">{{navItem[1]}}</i>{{navItem[0]}}</li>
在js文件中:
$scope.items = [
["Home", "home"],
["Favorite", "favorite"]
]