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

查看:97
本文介绍了角度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的值将使用新值进行更新,并在下一个循环中使用.我想一次循环两个元素,而不仅仅是一个.

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

推荐答案

如果问题仍然很重要,对于那些正在寻找答案的人,我建议您参考这篇文章:

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.

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

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