简体   繁体   English

Angular - *ngFor OUTSIDE of the *ngFor 的访问计数使用过滤器 pipe

[英]Angular - Access count of *ngFor OUTSIDE of the *ngFor using a filter pipe

           <div class="postings-list-results-number">
                {{resultsNumber}} results
            </div>
            <div class="postings-list-element" *ngFor="let posting of postingsIterableContent | async | postingsFilter:'54016'; let resultsNumber = count">
                <div class="postings-list-element-title">
                    {{posting.name}}
                </div>
             </div>

I just want to access the filtered results count OUTSIDE of the *ngFor.我只想访问 *ngFor 之外的过滤结果计数。 No I can not use postingsIterableContent.length because the lenght I get is the length before the pipe filtering.不,我不能使用 postsIterableContent.length 因为我得到的长度是 pipe 过滤之前的长度。 The only way to access the filtered pipe length is inside the *ngFor with the count variable.访问过滤后的 pipe 长度的唯一方法是在 *ngFor 中使用 count 变量。 I've searched almost every answer and I haven't found anything yet.我几乎搜索了所有答案,但还没有找到任何东西。

Some solutions could be to declare a variable inside the template in the outter scope and fill it inside the *ngFor but I haven't found anything yet.一些解决方案可能是在外部 scope 的模板内声明一个变量并将其填充到 *ngFor 中,但我还没有找到任何东西。 Another way I thought about is to call a function and set the resultsNumber property inside the controller but I can not make it work either.我想到的另一种方法是调用 function 并在 controller 内设置 resultsNumber 属性,但我也无法使其工作。 Does anyone have any idea?有人有什么主意吗? Of course I can not use an ng-container in the outter scope and put the *ngFor in there because I don't want my results number to be repeated in every element.当然,我不能在外部 scope 中使用 ng-container 并将 *ngFor 放在那里,因为我不希望我的结果编号在每个元素中都重复。

I found a solution thanks to Sanjay Choudhary's idea.感谢 Sanjay Choudhary 的想法,我找到了解决方案。 I do not know if it's the best approach but still it's the only one it's working.我不知道这是否是最好的方法,但它仍然是唯一有效的方法。 I set a template variable on the element I've used the *ngFor on.我在使用 *ngFor 的元素上设置了一个模板变量。

            <div #postingItems class="postings-list-element" *ngFor="let posting of postingsIterableContent | async | postingsFilter:'54016';">

I've accessed it in the.ts file using我已经在 .ts 文件中使用

  @ViewChildren('postingItems') postingItemsInDom: any;

and I finally display the length outside the *ngFor using我终于显示 *ngFor 之外的长度使用

<div class="postings-list-results-number">
                {{(postingItemsInDom)?.length}} results
  </div>

The only problem is that I get a console error core.js:6456 ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.唯一的问题是我得到一个控制台错误 core.js:6456 ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it is checked。 Previous value: 'null'.以前的值:'null'。 Current value: '0'..当前值:'0'..

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

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