在Angular 1中,有here的过滤器属性和 ng-repeat
.
但是在Angular 2中有一个名为"pipe"的新功能 . 我已经使用过管道,但仍然无法搜索Angular 1版本中的所有属性 .
但它适用于任何1个属性,但对于整个数组,它不起作用 .
当我搜索某些内容时出现以下错误 .
ERROR TypeError:无法在位于http:// localhost:8100 / build / main的Array.some(native)的http:// localhost:8100 / build / main.js:118751:76处读取属性'includes'的null . 在checkAndUpdatePureExpressionInline(http:// localhost:8100 / build / main)的FilterPipe.transform(http:// localhost:8100 / build / main.js:118750:26)的Array.filter(native)处的js:118751:42 . js:11519:38)checkAndUpdateNodeInline(http:// localhost:8100 / build / main.js:12374:17)at checkAndUpdateNode(http:// localhost:8100 / build / main.js:12336:16)debugCheckAndUpdateNode (http:// localhost:8100 / build / main.js:12965:59)在object.eval的debugCheckDirectivesFn(http:// localhost:8100 / build / main.js:12906:13)[作为updateDirectives](ng :///AppModule/Orders.ngfactory.js:628:46)在checkAndUpdateView(http://)的Object.debugUpdateDirectives [as updateDirectives](http:// localhost:8100 / build / main.js:12891:21) localhost:8100 / build / main.js:12303:14)在execComponentViewsAction(h)的callViewAction(http:// localhost:8100 / build / main.js:12618:17) ttp:// localhost:8100 / build / main.js:12564:13)错误语境DebugContext_ {view:Object,nodeIndex:43,nodeDef:Object,elDef:Object,elView:Object}组件:(...)componentRenderElement :(...)context:(...)elDef:ObjectbindingFlags:2bindingIndex:6bindings:Array(1)childCount:1childFlags:73728childMatchedQueries:0directChildFlags:73728element:Objectflags:1index:42matchedQueries:ObjectmatchedQueryIds:0ngContent:nullngContentIndex:0outputIndex:3outputs :Array(0)parent:Objectprovider:nullquery:nullreferences:ObjectrenderParent:Objecttext:null__proto :ObjectelOrCompView:(...)elView:Objectinjector:(...)nodeDef:ObjectnodeIndex:43providerTokens :( ...)references :( . ..)renderNode:(...)view:Object__proto:Object
以下是我的参考代码:
1)FilterPipe.ts
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform(items: any, term: any): any {
if (term) {
// THIS IS WORKING BUT FOR ONLY order_id PROPERTY
// return items.filter(function (item) {
// return item.order_id.toLowerCase().includes(term.toLowerCase());
// });
// THIS IS NOT WORKING
return items.filter(item => {
return Object.keys(item).some(k => item[k].includes(term.toLowerCase()));
});
}
return items;
}
}
2)Orders.html
<ion-header style="direction: ltr;">
<ion-navbar>
<ion-buttons left>
<button ion-button icon-only (click)="logout()">
<ion-icon name="log-out"></ion-icon>
</button>
</ion-buttons>
<ion-title>הזמנה</ion-title>
<ion-buttons start>
<button ion-button icon-only (click)="getOrders()">
<ion-icon name="refresh"></ion-icon>
</button>
</ion-buttons>
<ion-buttons end>
<button ion-button icon-only (click)="showFilterBar()">
<ion-icon name="search"></ion-icon>
</button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ul class="list" *ngFor="let order of orders | filter : Search.value">
<li class="item {{order.color}}" (click)="gotoorderdetails(order)">
<div style="float:left;">
{{order.start_date}}
</div>
<b>{{order.order_id}} </b> צ - {{order.total_staff}} ימ- {{order.number_of_days}}
{{order.full_name}}
<div style="float:left;">
{{order.managers}}
</div>
<span *ngIf="order.event_location"> {{order.event_location}}
</span>
</li>
</ul>
</ion-content>
<ion-footer>
<ion-searchbar #Search (keyup)="0"></ion-searchbar>
</ion-footer>
如何在离子2中使用ionic filter bar .
1 回答
谢谢JB Nizet的答案 .
以下是我工作的更新代码 .