如何使用字符串索引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>
答案 0 :(得分:3)
您可以对数组中的项使用普通语法。 请参考我的小提琴
<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>
答案 1 :(得分:1)
使用方括号访问对象/数组字段/元素。
<div ng-repeat="day in days">{{day}}
<span ng-repeat="item in arr[day]">{{item}}</span>
</div>
答案 2 :(得分:0)
您可以像ng-repeat="item in arr[day]"
答案 3 :(得分:0)
您也可以打印项目编号。
<div ng-repeat="day in days">
{{day}}
<div ng-repeat="(key, value) in arr[day]">Item #{{key + 1}}: {{value}}
</div>
<br />
</div>
答案 4 :(得分:0)
是的,这绝对是可能的。请参阅下面的工作示例:
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"];
});
&#13;
<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>
&#13;