英文:
How to start loop from index 1 instead of index 0 in *ngFor loop in angular?
问题
我目前从一个虚拟API获取一些数据,响应中的数据从1开始,索引目前从0开始。
如何将索引循环从1开始而不是0?
以下是*ngFor的HTML部分:
component.html
<div class="all-users" >
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index;" (click)="switchUsers(i)">
<h4>{{i + 1}}</h4>
<img src="{{data.image}}" alt="profile">
</div>
</div>
英文:
I am currently getting some data from a dummy api, data from response starts from 1 and the index is currently starting from 0.
How I can start the index loop from 1 instead of 0?
Following the html for *ngFor below:
component.html
<div class="all-users" >
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index; " (click)="switchUsers(i)">
<h4>{{i}}</h4>&nbsp;
<img src="{{data.image}}" alt="profile">
</div>
</div>
答案1
得分: 2
不要回答我要翻译的问题。以下是要翻译的内容:
"Why not just do the following:
<div class="all-users" >
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index; " (click)="switchUsers(i)">
<h4>{{i + 1}}</h4>&nbsp;
<img src="{{data.image}}" alt="profile">
</div>
</div>
I don't know of a way to change where the index starts, but this way works fine for most cases"
英文:
Why not just do the following:
<div class="all-users" >
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index; " (click)="switchUsers(i)">
<h4>{{i + 1}}</h4>&nbsp;
<img src="{{data.image}}" alt="profile">
</div>
</div>
I don't know of a way to change where the index starts, but this way works fine for most cases
答案2
得分: 1
如何从1而不是0开始索引循环
这里的正确答案是... 你无法这样做。
已经有一些建议了,但个人建议避免修改你的标记以绕过 API 返回的数据结构。让你的标记需要了解服务器的操作通常是一个不好的想法。
如果你的 API 返回的数组格式不适合你的标记 - 只需创建一个新的数据数组,并将你的标记指向它。最好在处理 API 结果的地方执行这个操作,但如果没有其他办法,可以在你的 .ts 文件中完成。
英文:
> How I can start the index loop from 1 instead of 0
The correct answer here is... you can't.
So there are some suggestions already, but personally I would avoid hacking your markup to get round the data structure the api returns. It's generally a bad idea to make your markup need to know too much about what your server does.
If the array your api returns is not in the best format for your markup - just create a new data array which is, and point your markup at that. It's best to do this in a service at the point you handle the api result, but if nothing else, do it in your .ts file.
答案3
得分: 0
Sure, here is the translated code:
你尝试过这样写吗:
<div class="all-users">
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index; " (click)="switchUsers(i+1)">
<h4>{{i+1}}</h4>&nbsp; <img src="{{data.image}}" alt="profile">
</div>
</div>
或者你可以跳过第一个元素,像这样写:
<div class="nested-items" *ngFor="let data of flattenedResponse[0] | slice:1; let i=index; " (click)="switchUsers(i)"></div>
Please note that I've retained the original HTML tags and attribute values while providing the Chinese translation for the code.
英文:
have you try :
<div class="all-users">
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index; " (click)="switchUsers(i+1)">
<h4>{{i+1}}</h4>&nbsp; <img src="{{data.image}}" alt="profile">
</div>
</div>
or you can just skip first element like this :
<div class="nested-items" *ngFor="let data of flattenedResponse[0] | slice:1; let i=index; " (click)="switchUsers(i)"></div>
答案4
得分: 0
你可以明确使用 *ngIf
来跳过第一个索引:
<div class="all-users">
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index;" (click)="switchUsers(i)">
<ng-container *ngIf="index !== 0">
<h4>{{i}}</h4>&nbsp;
<img src="{{data.image}}" alt="profile">
</ng-container>
</div>
</div>
英文:
You can skip first index explicitly with *ngIf
:
<div class="all-users" >
<div class="nested-items" *ngFor="let data of flattenedResponse[0]; let i=index;" (click)="switchUsers(i)">
<ng-container *ngIf="index !== 0">
<h4>{{i}}</h4>&nbsp;
<img src="{{data.image}}" alt="profile">
</div>
</ng-container>
</div>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论