角度 2 如何绑定 div 元素


 <div *ngFor="let city of 
   israelCitieService.data|mychoosedcities:wordSerched;let i=index"
 </div>

我想知道从管道返回的数组的长度。 索引 (i( 在div 元素之外是未知的。 我用我的组件绑定它有多大?提前致谢

试试这个:

<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 ;
}

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

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

最新更新