Angular 2如何绑定div元素?

问题描述 投票:0回答:2
 <div *ngFor="let city of 
   israelCitieService.data|mychoosedcities:wordSerched;let i=index"
 </div>

我想知道从管道返回的数组的长度。并且索引(i)在div元素之外是未知的。我怎么把它绑在我的组件上?提前致谢

angular typescript
2个回答
0
投票

试试这个:

<div *ngFor="let cityofisraelCitieService.data|mychoosedcities:wordSerche as array;let i=index">
  <div (click)="passIndexValue(array)"></div> 
<div>
<div>{{lenght}}</div>

在您的组件中:

passIndexValue(array){
   this.lenght = array.length ;
}

0
投票

我建议遵循Angular团队关于不使用管道进行数据操作的建议:https://angular.io/guide/pipes#appendix-no-filterpipe-or-orderbypipe

因此,将过滤器逻辑移动到控制器,将数据存储在变量上也可以正确地更新长度!如果您随后负责更改检测并在循环中添加索引,这应该可以为您提供最佳性能!

© www.soinside.com 2019 - 2024. All rights reserved.