Angular 2 搜索过滤器适用于一个 属性 但不适用于数组的所有属性
Angular 2 search filter works for one property but not for all properties of an array
在 Angular 1 中,如 here.
所述,ng-repeat
具有过滤器属性
但在 Angular 2 中有一个名为 "pipe" 的新功能。我已经使用过管道,但仍然无法像 Angular 1 版本那样搜索所有属性。
然而,它对任何 1 个 属性 都工作正常,但对整个数组,它不起作用。
我搜索东西时出现以下错误。
ERROR TypeError: Cannot read property 'includes' of null
at http://localhost:8100/build/main.js:118751:76
at Array.some (native)
at http://localhost:8100/build/main.js:118751:42
at Array.filter (native)
at FilterPipe.transform (http://localhost:8100/build/main.js:118750:26)
at checkAndUpdatePureExpressionInline (http://localhost:8100/build/main.js:11519:38)
at checkAndUpdateNodeInline (http://localhost:8100/build/main.js:12374:17)
at checkAndUpdateNode (http://localhost:8100/build/main.js:12336:16)
at debugCheckAndUpdateNode (http://localhost:8100/build/main.js:12965:59)
at debugCheckDirectivesFn (http://localhost:8100/build/main.js:12906:13)
at Object.eval [as updateDirectives] (ng:///AppModule/Orders.ngfactory.js:628:46)
at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:8100/build/main.js:12891:21)
at checkAndUpdateView (http://localhost:8100/build/main.js:12303:14)
at callViewAction (http://localhost:8100/build/main.js:12618:17)
at execComponentViewsAction (http://localhost:8100/build/main.js:12564:13)
ERROR CONTEXT DebugContext_ {view: Object, nodeIndex: 43, nodeDef: Object, elDef: Object, elView: Object}component:
(...)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}}<br/>
</div>
<b>{{order.order_id}} </b> צ - {{order.total_staff}} ימ- {{order.number_of_days}}<br/><br/> {{order.full_name}}
<br/>
<div style="float:left;">
{{order.managers}}<br/>
</div>
<span *ngIf="order.event_location"> {{order.event_location}}<br/></span>
</li>
</ul>
</ion-content>
<ion-footer>
<ion-searchbar #Search (keyup)="0"></ion-searchbar>
</ion-footer>
如何在 ionic 2 中使用 ionic filter bar。
感谢 JB Nizet 的回答。
下面是我的工作更新代码。
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform(items: any, term: any): any {
if (term) {
return items.filter(item => {
return Object.keys(item).some(
k => {
if (item[k] != null && item[k] != undefined && typeof item[k] == 'string')
return item[k].includes(term.toLowerCase());
}
);
});
}
return items;
}
}
在 Angular 1 中,如 here.
所述,ng-repeat
具有过滤器属性
但在 Angular 2 中有一个名为 "pipe" 的新功能。我已经使用过管道,但仍然无法像 Angular 1 版本那样搜索所有属性。
然而,它对任何 1 个 属性 都工作正常,但对整个数组,它不起作用。
我搜索东西时出现以下错误。
ERROR TypeError: Cannot read property 'includes' of null at http://localhost:8100/build/main.js:118751:76 at Array.some (native) at http://localhost:8100/build/main.js:118751:42 at Array.filter (native) at FilterPipe.transform (http://localhost:8100/build/main.js:118750:26) at checkAndUpdatePureExpressionInline (http://localhost:8100/build/main.js:11519:38) at checkAndUpdateNodeInline (http://localhost:8100/build/main.js:12374:17) at checkAndUpdateNode (http://localhost:8100/build/main.js:12336:16) at debugCheckAndUpdateNode (http://localhost:8100/build/main.js:12965:59) at debugCheckDirectivesFn (http://localhost:8100/build/main.js:12906:13) at Object.eval [as updateDirectives] (ng:///AppModule/Orders.ngfactory.js:628:46) at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:8100/build/main.js:12891:21) at checkAndUpdateView (http://localhost:8100/build/main.js:12303:14) at callViewAction (http://localhost:8100/build/main.js:12618:17) at execComponentViewsAction (http://localhost:8100/build/main.js:12564:13)
ERROR CONTEXT DebugContext_ {view: Object, nodeIndex: 43, nodeDef: Object, elDef: Object, elView: Object}component: (...)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}}<br/>
</div>
<b>{{order.order_id}} </b> צ - {{order.total_staff}} ימ- {{order.number_of_days}}<br/><br/> {{order.full_name}}
<br/>
<div style="float:left;">
{{order.managers}}<br/>
</div>
<span *ngIf="order.event_location"> {{order.event_location}}<br/></span>
</li>
</ul>
</ion-content>
<ion-footer>
<ion-searchbar #Search (keyup)="0"></ion-searchbar>
</ion-footer>
如何在 ionic 2 中使用 ionic filter bar。
感谢 JB Nizet 的回答。
下面是我的工作更新代码。
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter'
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform(items: any, term: any): any {
if (term) {
return items.filter(item => {
return Object.keys(item).some(
k => {
if (item[k] != null && item[k] != undefined && typeof item[k] == 'string')
return item[k].includes(term.toLowerCase());
}
);
});
}
return items;
}
}