admin管理员组文章数量:1356304
I am creating a table using Clarity.
They have their own syntax, clrDgItems
, but the structure is the same as ngFor.
I want to create an loop for my table, which filters the first 10 items, and then a second table which only shows items 11-20.
You should also be able to go to the next page. That will say, first table to update to items 21-30 and second table to 31-40
My first thoughts was something like,
ngFor="let employee of employees; i as index; i < firstMaxIndex;"
ngFor="let employee of employees; i as index; i > firstMaxIndex && i < secondMaxIndex"
But ngFor loop doesnt work like that, and I get the error: NG5002: Parser Error: Unexpected token <
I am creating a table using Clarity.
They have their own syntax, clrDgItems
, but the structure is the same as ngFor.
I want to create an loop for my table, which filters the first 10 items, and then a second table which only shows items 11-20.
You should also be able to go to the next page. That will say, first table to update to items 21-30 and second table to 31-40
My first thoughts was something like,
ngFor="let employee of employees; i as index; i < firstMaxIndex;"
ngFor="let employee of employees; i as index; i > firstMaxIndex && i < secondMaxIndex"
But ngFor loop doesnt work like that, and I get the error: NG5002: Parser Error: Unexpected token <
- I suppose next statement can be condition, would that won't help instead of clubbing them together? – Bharat Commented Jul 20, 2021 at 7:08
- Do you might explain more detail? – Apple Yellow Commented Jul 20, 2021 at 7:08
-
The condition should go inside
*ngFor
context, like this:<div *ngIf="i < firstMaxIndex">...</div>
– lbsn Commented Jul 20, 2021 at 7:09 - @Bharat You right, I am used to for-loops in C#, where the condition and declare is in the same parameters! – Robin Lindgren Commented Jul 20, 2021 at 7:12
- 1 @Abinesh already posted it :). To me it makes more clean and readable code, like splitting the loop and condition as two different responsibilities. – Bharat Commented Jul 20, 2021 at 7:17
2 Answers
Reset to default 4It's better to separate out the condition and loop as below:
<ng-container *ngFor="let employee of employees; i as index;">
<div *ngIf="i > firstMaxIndex && i < secondMaxIndex">
// add elements or display values based on your needs.
</div>
</ng-container>
Use slice
{{ value_expression | slice : start [ : end ] }}
So like this:
ngFor="let employee of employees | slice : 0 : firstMaxIndex"
ngFor="let employee of employees | slice : firstMaxIndex : secondMaxIndex"
本文标签: javascriptHow to add a condition for ngFor loopStack Overflow
版权声明:本文标题:javascript - How to add a condition for ngFor loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744062289a2584347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论