gpt4 book ai didi

javascript - AngularJs:ng-重复 ="item in arrayValue"

转载 作者:行者123 更新时间:2023-11-29 10:41:16 24 4
gpt4 key购买 nike

我有以下数组:

 var date = {
'2015': [{
'month': 'Januar',
'day': 31
}, {
'month': 'Februar',
'day': 28
}]
};

我想让 ng-repeat 等于天数:
需要的输出:

<div ng-repeat="date in date.2015">
{{date.month}}
<!-- Displays 12 month name -->
<div ng-repeat="day in date.2015+{{$index}}+'.day'">
{{day}}
<!-- displays 31, 30 or 28 times the day -->
<!-- The day should also increment from 0 to the number -->
</div>
</div>

我希望这是清楚的。
ng-repeat="day in date.2015+{{$index}}+'.day'" 不起作用

最佳答案

首先,您需要在您的 Controller 中使用一个辅助函数,将您的数字转换为一个长度等于您一天的值的数组:

$scope.range = function(n) {
return new Array(n);
}

如果您使用的是最新版本的 Angular,则需要在 repeat 中明确设置一个变量为别名。 Working fiddle with the code you provided

<div ng-repeat="date in date.2015">
{{date.month}} <!-- Displays 12 month name -->
<div ng-repeat="day in range(date.day) track by $index">
{{$index + 1}} <!-- displays 31, 30 or 28 times the day -->
<!-- The day should also increment from 0 to the number -->
</div>
</div>

然后您需要调整您的第二个 ng-repeat 以利用 Controller 的功能来获取 ng-repeat 的索引并添加 1,因为它是基于 0 的。 Working Plunker with old version

<div ng-repeat="date in date.2015">
{{date.month}} <!-- Displays 12 month name -->
<div ng-repeat="day in range(date.day)">
{{$index + 1}} <!-- displays 31, 30 or 28 times the day -->
<!-- The day should also increment from 0 to the number -->
</div>
</div>

关于javascript - AngularJs:ng-重复 ="item in arrayValue",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28650231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com