繁体   English   中英

角度:使用* ngFor和* ngIf引用子元素中的索引值

[英]Angular: using both *ngFor and *ngIf with reference to index value in child elements

我想在某些网格车组件中呈现可观察到的数组对象 另外,我想根据组件中的变量(即使用*ngIf条件检查的gridCols*maxRows将网格卡的数量限制为一个值。

我知道不可能在同一元素上同时使用*ngFor*ngIf 所以我需要使用<template ngFor ...>包装我的网格卡元素,这些元素将根据*ngFor有条件地呈现。 鉴于此,我如何从*ngFor中的*ngIf引用indexcontent变量。

<template ngFor let-content [ngForOf]="contentObjectsObservable | async" let-i="index" [ngForTrackBy]="trackByFn">
    <grid-card *ngIf="i < gridCols*maxRows" [content]="content" [style.width.%]="gridCardWidth"></grid-card>
</template> 

更新

我已经尝试过这样的事情

 #i="index"

与这样的错误信息:

没有将“ exportAs”设置为“ index”的指令

您可以通过将let i = index放在* ngFor语句中来从* ngFor引用索引! 例:

<div *ngFor="let item of items; let i = index">
    <div *ngIf="i < (gridCols * maxRows)" class="row">
    <grid-card [content]="content" [style.width.%]="gridCardWidth"></grid-card>
    </div>
</div>

确保已在组件中将gridCols和maxRows定义为数字!

gridCols:number = <your number>;
maxRows:number = <your number>;

希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM