gpt4 book ai didi

javascript - AngularJS 反转项目列表显示非反转索引编号

转载 作者:行者123 更新时间:2023-11-30 17:22:20 25 4
gpt4 key购买 nike

我使用关于反向过滤器的许多堆栈溢出问题之一编写了一个过滤器来反向列表。

我的代码:

<div ng-repeat="query in queries | reverse">
<p>Submission: {{$index}}</p>
<p>Title: {{query.title}}</p>
<p>Body: {{query.response}}</p>
</div>

数组:

var queries = [
{ title: "Query type A", body: "Response of query type A" },
{ title: "Query type B", body: "Response of query type B" },
{ title: "Query type C", body: "Response of query type C" }
];

现在我的列表被颠倒了,我想显示每个项目的计数,所以它看起来像下面这样:

Submission: 3
Title: Query type C
Body: Response of query type C

Submission: 2
Title: Query type B
Body: Response of query type B

Submission: 1
Title: Query type A
Body: Response of query type A

相反,顺序是正确颠倒的,但是提交编号(就是$index,是倒过来的:

Submission: 1
Title: Query type C
Body: Response of query type C

Submission: 2
Title: Query type B
Body: Response of query type B

Submission: 3
Title: Query type A
Body: Response of query type A

我意识到这在某种程度上可能是正确的行为,但我如何反向显示该数字?

最佳答案

$index 只是遍历循环中的项目。它不知道逆转。您可以在查询中使用“id”元素,例如:

var queries = [
{ title: "Query type A", body: "Response of query type A", id:1 },
{ title: "Query type B", body: "Response of query type B", id:2 },
{ title: "Query type C", body: "Response of query type C", id:3 }
];

并使用<p>Submission: {{query.id}}</p>而不是 <p>Submission: {{$index}}</p> .

关于javascript - AngularJS 反转项目列表显示非反转索引编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918209/

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