gpt4 book ai didi

Angularjs ng-repeat数组中的数组

转载 作者:行者123 更新时间:2023-12-01 12:36:39 26 4
gpt4 key购买 nike

我想在列表中显示另一个数组中的一个数组。我想使用 ng-repeat 在列表中显示数组。我尝试了各种组合,但我无法让它发挥作用。我有以下代码:

var workouts = this;  
workouts.workoutslist = [
{'workoutId': 1,
'workoutName': "Maandag",
'exercises':
[
{exerciseId: 1,
exerciseName: "exercise1"},
{exerciseId: 2,
exerciseName: "exercise2"}
]
},
{
'workoutId': 2,
'workoutName': "Dinsdag",
'exercises':
[
{exerciseId: 3,
exerciseName: "exercise3"},
{exerciseId: 4,
exerciseName: "exercise4"}
]
}
];

这个有效:

<li class="item" ng-repeat="workoutTest in workout.workoutslist[0].exercises">{{ workoutTest.exerciseName }}
</li>

但这不是:

    <li class="item" ng-repeat="workoutTest in workout.workoutslist.exercises  |filter:{workoutId: workoutId}">
{{ workoutTest.exerciseName }}
</li>

我在第二个代码中说的不是完全一样吗?
编辑:Plunker:http://plnkr.co/edit/1VQBkuTWAVatexPMsxOd?p=preview

最佳答案

问题说明:

之所以有效,是因为它针对的是单次锻炼的练习:

workoutTest in workout.workoutslist[0].exercises

这行不通:

workoutTest in workout.workoutslist.exercises | filter:{workoutId: workoutId}

因为过滤器应用于锻炼列表数组的不存在属性“练习”

解决方案一:

如果您想显示由 id 选择的单个锻炼的练习 - 尝试将您的锻炼定义为这样的对象:

workouts.workoutslist = { 
"1": {
'workoutName': "Maandag",
'exercises': [
{exerciseId: 1, exerciseName: "exercise1"},
{exerciseId: 2, exerciseName: "exercise2"}
]
},
"2": {
'workoutName': "Dinsdag",
'exercises': [
{exerciseId: 3, exerciseName: "exercise3"},
{exerciseId: 4, exerciseName: "exercise4"}
]
}
};

并像这样访问它:

exercise in workout.workoutslist[workoutId].exercises

解决方案 2:

如果您想显示包含练习列表的锻炼列表,而不是像这样实现两级列表:

<ul>
<li ng-repeat="w in workout.workoutslist">
<span>{{w.workoutName}}<span>
<ul>
<li ng-repeat="e in w.exercises">{{e.exerciseName}}</li>
</ul>
</li>
</ul>

关于Angularjs ng-repeat数组中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028840/

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