Arrays 角度-通过按钮显示数组列表中的隐藏值
我正在处理web应用程序中的值列表。在这里,我面临着以下问题: app.component.html 这部分代码以列表的形式显示我的数组:Arrays 角度-通过按钮显示数组列表中的隐藏值,arrays,angular,list,Arrays,Angular,List,我正在处理web应用程序中的值列表。在这里,我面临着以下问题: app.component.html 这部分代码以列表的形式显示我的数组: 您可以将5保留在某个变量中,然后单击“ShowAll()”,您可以使用*ngIf来增加限制。 您应该将数组切片到5,并存储到另一个变量中。 用户单击按钮以显示所有数据后,将整个数组分配给新变量 public showAll(){ this.newData = this.data; } 例如: this.data = [1,2,3,4,5
您可以将5保留在某个变量中,然后单击“ShowAll()”,您可以使用
*ngIf
来增加限制。
您应该将数组
切片到5,并存储到另一个变量中。
用户单击按钮以显示所有数据后,将整个数组
分配给新变量
public showAll(){
this.newData = this.data;
}
例如:
this.data = [1,2,3,4,5,6,7,8,9,10];
this.newData = data.slice(0,5);
然后在component.html
文件中:
<div class="body__tags">
<ng-container *ngFor="let tag of newData; let i=index">
<li class="tags__list" [label]="tag"></li>
</ng-container>
</div>
谢谢!很好用!当我再次点击按钮时,我需要添加什么来隐藏其他的?再次切片
数据
并分配给新数据
你能举个例子吗?@macintosh11你可以引入一个布尔
标志isAllVisible
。当标志为true
时,切片数组并分配给newData
,否则分配整个数据。最后,切换
时,将全部可见
标志值。
<div class="body__tags">
<ng-container *ngFor="let tag of newData; let i=index">
<li class="tags__list" [label]="tag"></li>
</ng-container>
</div>
public showAll(){
this.newData = this.data;
}