简体   繁体   English

如何在angular2中的ngFor中显示1个元素?

[英]How to show 1 element in ngFor in angular2?

I have a simple ngFor that loops through an array. 我有一个简单的ngFor,它遍历一个数组。 However, I've added a condition that while the index is < 5 keep add the tag. 但是,我添加了一个条件,即当索引< 5请添加标签。 and After that, I want to add an extra tag just once that will be used a dropdown to view the rest of the tags. 之后,我只想添加一个额外的标签,然后将其用于下拉菜单以查看其余标签。 But it doesn't work. 但这是行不通的。

 <li *ngFor="let tag of module.Tags; let i = index"> <a *ngIf="i<5" href="#" class="span-tag tag">{{ tag }}</a> <div *ngIf="module.Tags.length > 5 && i == 6">DropDown Button</div> </li> 

The feature here is that I don't want to show unlimited number of tags to the user, I want to limit it to only 5 tags, and have a button after 5 tags which will be used to show the dropdown with the remaining tags. 这里的功能是我不想向用户显示无限数量的标签,我希望将其限制为仅5个标签,并在5个标签之后有一个按钮,该按钮将用于显示其余标签的下拉列表。

Is this possible to do in angular2? 在angular2中可以这样做吗?

If so, please enlighten me. 如果是这样,请赐教。

<li *ngFor="let tag of module.Tags | slice:0:5; let last=last">
  <a href="#" class="span-tag tag">{{ tag }}</a>
  <div *ngIf="last">DropDown Button</div>
</li>

https://angular.io/docs/ts/latest/api/common/index/SlicePipe-pipe.html https://angular.io/docs/ts/latest/api/common/index/SlicePipe-pipe.html

To get all added but the <div>DropDown Button</div> added after the 5th item you can use: 要获得所有添加的内容,但要在第五项之后添加<div>DropDown Button</div> ,可以使用:

show = 5;

<li *ngFor="let tag of module.Tags|slice:0:show let i=index">
  <a href="#" class="span-tag tag">{{ tag }}</a>
  <div *ngIf="i==4 && show == 5" (click)="show = module.Tags.length">DropDown Button</div>
</li>

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

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