AngularJS-ng使用字符串索引在数组上重复
AngularJS - ng-repeat over array with string index
如何在具有字符串索引的数组上ng-repeat
?请参阅下面的代码片段-
以下代码位于控制器中。
$scope.days = ["mon", "tue", "wed" .... "sun"];
$scope.arr = [];
$scope.arr["mon"] = ["apple","orange"];
$scope.arr["tue"] = ["blue","swish"];
.
.
.
$scope.arr["sun"] = ["pineapple","myfruit","carrot"];
问题-如何像下面这样ng-repeat
,有可能吗?
<div ng-repeat="day in days">{{day}}
<span ng-repeat="item in arr(day)">{{item}}</span>
<-- Something like "arr(day)", can it be done in angular -->
</div>
您可以对数组中的项使用普通语法。请参考我的小提琴
<div ng-app='app' ng-controller='default' ng-init='init()'>
<div ng-repeat='day in days'>
<strong>{{day}}</strong><br/>
<span ng-repeat="item in arr[day]">{{item}} </span>
</div>
</div>
https://jsfiddle.net/DoTH/evcv4tu5/3/
使用方括号访问对象/数组字段/元素。
<div ng-repeat="day in days">{{day}}
<span ng-repeat="item in arr[day]">{{item}}</span>
</div>
您可以像这样使用ng-repeat="item in arr[day]"
您也可以打印项目编号。
<div ng-repeat="day in days">
{{day}}
<div ng-repeat="(key, value) in arr[day]">Item #{{key + 1}}: {{value}}
</div>
<br />
</div>
是的,这是绝对可能的。参见下面的工作示例:
var app = angular.module("sa", []);
app.controller("FooController", function($scope) {
$scope.days = ["mon", "tue", "wed", "sun"];
$scope.arr = [];
$scope.arr["mon"] = ["apple", "orange"];
$scope.arr["tue"] = ["blue", "swish"];
$scope.arr["sun"] = ["pineapple", "myfruit", "carrot"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="sa" ng-controller="FooController">
<ol>
<li ng-repeat="day in days">
<strong>{{day}}</strong>
<ol>
<li ng-repeat="item in arr[day]">{{item}}</li>
</ol>
</li>
</ol>
</div>
相关文章:
- 按最大值获取数组索引
- 删除和重用数组索引
- 警报数组索引和值
- .fadeIn .fadeOut 会导致数组索引出现奇怪的行为
- Javascript原型通用Enquries和通过数组索引分配Id
- 获取数组索引的值
- Javascript-将值与关联数组索引进行比较
- Javascript-在数组索引中循环的问题
- JavaScript - “规范化”数组索引
- 数组索引混淆
- 使用 Javascript/React.js 查找对象的数组索引
- 如何拆分数组索引
- 获取新推送元素的数组索引
- 在 javascript 中将数组索引作为参数传递
- 当对象本身存储在 JavaScript 的数组索引中时,如何打印对象的每个单独属性
- 如何使用数组索引序列
- ASP.NET/JavaScript:数组索引溢出:实体框架/LINQ
- 如何在分配给数组索引的对象上调用方法
- 使用铆钉访问数组索引.js
- 计算 Javascript 数组索引的出现次数