Angular 6:存储在 *ngFor 中使用的值 [英] Angular 6: Storing value to use in *ngFor

查看:24
本文介绍了Angular 6:存储在 *ngFor 中使用的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面代码的循环体中:

In the loop body in the code below:

<ul>
   <li *ngFor="let article of articles; index as i">
       <a>{{ articles[i].title }} -- {{i}}</a>
       <p>{{ articles[i].body }}</p>
       <!-- i++ -->  
       <p>{{ i }}</p>
   </li>
</ul>

<小时>

有没有办法在那个位置存储i++的值,然后打印出i的新值?


Is there anyway to store the value of i++ at that position, then print out new value of i?

然后,i 的值被更新为新值并用于下一个循环.我想一次循环 2 个元素,而不仅仅是一个.

Then, i's value is updated with the new value and used in the next loop. I would like to loop 2 elements at a time, not just one.

就像我们在 Java 中可以做的一样:

It's just like what we can do in Java as below:

for (int i = 0; i < 10; i++) {
    System.out.println(i);
    i++;
    System.out.println(i);
}

第一个循环:

0

1

第二个循环:

2

3

等等..

我找到了解决方案,在此链接中:使用 *ngFor

I found the solution, which is in this link: Looping through list two at a time using *ngFor

解决方案:将数组拆分为二维数组,使用ngFor很容易循环

Solution: split array into a 2-dimensional array, it's easy to loop using ngFor

推荐答案

如果问题仍然相关,对于那些正在寻求答案的人,我建议您参考这篇文章:使用 *ngFor 一次循环两个列表

If question stays relevant, for those who are seeking the answer, I would recommend you to refer to this post: Looping through list two at a time using *ngFor

这篇文章不是我的,但它展示了一个使用管道的简单解决方案.

The post is not mine, but it shows a simple solution using the pipeline.

这篇关于Angular 6:存储在 *ngFor 中使用的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆