Angular 如何根据选定的下拉值筛选*NGF结果
选择类别:Angular 如何根据选定的下拉值筛选*NGF结果,angular,ionic-framework,ionic3,ngfor,Angular,Ionic Framework,Ionic3,Ngfor,选择类别: <ion-select name="categories"> <ion-option *ngFor="let category of categories;"> {{category}} </ion-option> </ion-select> {{category}} 列出项目: <ion-item-sliding *ngFor="let item of items; let idx =
<ion-select name="categories">
<ion-option *ngFor="let category of categories;">
{{category}}
</ion-option>
</ion-select>
{{category}}
列出项目:
<ion-item-sliding *ngFor="let item of items; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
{{item.title}
...
如何使用(选择)中的值来选择列出的类别?我尝试在中添加一个ngModule并将其作为条件传递到滑动ngFor中,但它不起作用 您可以使用管道来实现这一点
<ion-item-sliding *ngFor="let item of items | categorySelected; let idx = index;">
<ion-item>
<h2>{{item.title}}</h2>
</ion-item>
...
</ion-item-sliding>
更新1:完整答案
自定义管道将具有以下代码
transform(records: Array<any>, property:any): any {
let sortedArray=[];
if(property){
sortedArray =_.filter(records, {'office':property.Name]);
console.log(sortedArray);
return sortedArray
}
}
转换(记录:Array您也可以简单地
ngOnChanges() {
this.itemsForDisplay = this.items.filter(element => element.category = this.category);
}
并在模板中显示itemsForDisplay
,而不是items
您可以使用库在数组和字符串中进行难以置信的过滤和转换
对于您的问题,您可以使用ngx管道
库中的filterBy管道
<ion-option *ngFor="let category of categories | filterBy:['name']:selectedVariable;">
你是对的,pipe
是正确的选择。但也许一个更可靠的例子会对这家伙有更大的帮助。pipe不应该收到参数吗?这个答案不完整。我可能已经向pipe发送了输入。@Jota.Toledo检查更新的答案。另请参阅demo.sortedArray=\uu.filter(记录,{'office':property.Name]);未定义下划线。
ngOnChanges() {
this.itemsForDisplay = this.items.filter(element => element.category = this.category);
}
<ion-option *ngFor="let category of categories | filterBy:['name']:selectedVariable;">