Angular6 array.filter不返回筛选结果
在我的angular 6应用程序中,我正在列表中搜索,所以在HTML中我有下面的代码Angular6 array.filter不返回筛选结果,angular6,Angular6,在我的angular 6应用程序中,我正在列表中搜索,所以在HTML中我有下面的代码 <li *ngFor="let c of listName | search: searchText ; let i = index;" class="advance-list-heading" > <span *ngIf = "i==0" class="test">My List</span>
<li *ngFor="let c of listName | search: searchText ; let i = index;" class="advance-list-heading" >
<span *ngIf = "i==0" class="test">My List</span>
<ul class="advance-list-treeview">
<li (click)="SetValue(c.name)">
class="row"> -->
<span *ngIf="c.name; else noresult">{{c.name}}</span>
<!-- </div> -->
</li>
</ul>
</li>
但是,页面中的现有列表未得到筛选。有人可以在此帮助我吗?而不是
return this.listName.filter((x: any) => x.name.toLowerCase().startsWith(SelectedList.toLowerCase()))
试一试
编辑:
然后
这可以工作并给我更新的结果,但当用户在searchbox中键入时,它会在更新的结果中搜索,而不是在原始结果集中搜索(即加载到ngOnInit上)@hdc尝试两个数组,list和filteredList。根据您的方便使用它们。如果您能更具体一些,请?我有绑定到HTML的listName数组,我如何在这里有两个数组,这有助于我在ngOnInit()上构建初始数组我想确保当用户搜索时,它应该自动筛选出页面加载时加载的数组。然而,当我再次单击文本框并再次搜索时,它会工作。我的意思是,
listName
可以绑定到HTML,但在函数内部过滤时,您可以使用类似于completeList
的数组,它包含API调用的所有结果。API调用成功时,this.completeList=reponse from API;this.listName=this.completeList
。然后在函数调用中this.listName=this.completeList.filter((x:any)=>x.name.toLowerCase().startsWith(SelectedList.toLowerCase())
return this.listName.filter((x: any) => x.name.toLowerCase().startsWith(SelectedList.toLowerCase()))
this.listName = this.listName.filter((x: any) => x.name.toLowerCase().startsWith(SelectedList.toLowerCase()))
ngOnInit(){
const response = this.getData();
this.completeList = responsee;
this.listName = this.completeList;
}
SetValue(SelectedList:string) {
this.inputField.nativeElement.value= SelectedList
this.divVisible = false;
this.listName = this.completeList.filter((x: any) => x.name.toLowerCase().startsWith(SelectedList.toLowerCase()));
}