过滤的NGFOR ANGULAR计数
是否有一种更简单的方法来获取具有嵌套NGIF的NGFOR中实际显示的结果的计数?
<div *ngFor="let stuff of things">
<ng-container *ngIf="stuff.key === 'value'">
**I'd like an easier way to get the result of this *ngIf .length and use it elsewhere**
</ng-container>
</div>
我试图使用@viewchildren并希望;
@ViewChildren('myContainerRef') containerRef: QueryList<any>
<label>Count {{ myContainerRef.length }}</label>
<div #myRef *ngFor="let stuff of things">
</div>
但是获得不确定的结果。我的最后一个度假胜地是在传入阵列上进行过滤器以获取计数并分配公共vars吗?还是有一种更简单的方法来获取符合条件 /显示的NGFOR结果的计数?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将更改逻辑以允许过滤在.ts文件中,然后您具有峰值和计数...
以下允许过滤原始数组,并且长度是匹配项的数量
I would change the logic to allow the filtering to occur in the .ts file and then you have both the iterable and the count...
The following allows the filtering of the original array and the length is the number of matching items
您可以尽早使用
AfterviewInit
中的ViewChildren。您可以搜索ng-container
,但最好搜索使用#myref
的元素,因此它仅计算您感兴趣的元素。工作示例:
You can use ViewChildren in
AfterViewInit
at the earliest. You can search forng-container
but would be better to search the elements you're after with#myRef
so it would count only the ones you are interested in.Working example: https://stackblitz.com/edit/angular-ivy-vpq5n4?file=src%2Fapp%2Fapp.component.ts
您可以创建像这样的管道
,然后在模板
管中使用它,将返回过滤的数组,并且由于NGFOR提供了算作的导出值之一此处的文档您可以在ngfor表达式中分配到模板变量(length = count)
You can create pipe like this
then use it inside template
Pipe will return filtered array, and as ngFor provides count as one of it's exported values docs here you can assign in to template variable (length = count) inside ngFor expression