这是我的(custom.component.html)文件
<input ng-model="searchText" placeholder=" Enter the name"
class="seacrh-field"><br><br>
<mat-card class="example-card" *ngFor="let spaceScreen of
spaceScreens;
let i = index">
<mat-card-header>
<div mat-card-avatar class="example-header-image" >
<img mat-card-image class="list-img" src="
{{spaceScreen?.img}}" >
</div>
<mat-card-content class="names">{{ spaceScreen?.name }}
</mat-card-content>
</mat-card-header>
</mat-card>
这是(custom.component.ts)文件
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { map } from 'rxjs/operators'
@Component({
selector: 'ylb-customer',
templateUrl: './customer.component.html',
styleUrls: ['./customer.component.css']
})
export class CustomerComponent {
spaceScreens: Array<any>;
constructor(private http:Http){
this.http.get('assets/app.json').pipe(
map(response => response.json().screenshots)
).subscribe(res => this.spaceScreens = res);
}
}
此为(app.json)文件存在于assets文件夹中
{
"screenshots":[
{
"img":"assets/img/json_2.jpg",
"name":"Virat Kohli"
},
{
"img":"assets/img/json_2.jpg",
"name":"Joe Root"
},
{
"img":"assets/img/json_2.jpg",
"name":"Adam Gilchrist"
},
{
"img":"assets/img/json_2.jpg",
"name":"Kevin Peterson"
},
{
"img":"assets/img/json_2.jpg",
"name":"Misbhah-Ul-Hak"
},
{
"img":"assets/img/json_2.jpg",
"name":"ABD Develliers"
},
{
"img":"assets/img/json_2.jpg",
"name":"Ben stokes"
},
{
"img":"assets/img/json_2.jpg",
"name":"Chris Gayle"
}
]
}
一切都很好,我如何应用搜索过滤器(如手机中的联系人列表)到app.json file.Tried中的数据有很多方法,仍然没有result.How我可以使用自定义管道轻松实现
发布于 2018-08-04 04:46:29
在Angular 1中,大多数人使用管道进行过滤。这是由trichetriche在另一个答案的评论中指出的,是正确的。问题是允许这种行为会导致很差的性能,因为每个摘要周期都会触发过滤(这种情况经常发生)。因此,在Angular 2+中,他们希望您过滤组件中的结果并将结果存储在一个数组中,然后只需使用*ngFor="myFilteredArray“。
First setup binding on your input,这样我们就可以获得用户想要搜索的内容。
//Use this method if you want to track name for more than a filter.
//Result will be stored in name var
<input type="text" [(ngModel)]="name" (ngModelChange)="onNameChange()">
//Use this if you don't need to track the name text for anything else.
<input type="text" (input)="onNameChangeInput($event.target.value)">
<div *ngFor="let s of filteredScreenshots">
{{s | json}}
</div>
Second为您的component.ts添加更改函数
您将需要一个库,如lodash或underscore。如果您没有使用- https://lodash.com/docs安装lodash
npm install lodash
component.ts
import * as _ from 'lodash';
export class CustomerComponent {
spaceScreens: Array<any>;
filteredScreens = [];
name: string;
constructor(private http:Http){
this.http.get('assets/app.json').pipe(
map(response => response.json().screenshots)
)
.subscribe(res => {
this.spaceScreens = res; //this is what we filter against
this.filteredScreens = res; //init with full list
});
}
onNameChange() {
this.filteredScreens = _.filter(this.spaceScreens, (screenshot) => {
const name = screenshot['name'];
const filteredName = this.name.toUpperCase();
return name === undefined ? false : name.toUpperCase().startsWith(filteredName);
});
}
onNameChangeInput(filteredName: string) {
this.filteredScreens = _.filter(this.spaceScreens, (screenshot) => {
const name = screenshot['name'];
filteredName = filteredName.toUpperCase();
return name === undefined ? false : name.toUpperCase().startsWith(filteredName);
});
}
}
您只需使用一个输入和一个change函数,它们的名称都是适当的,因此每个输入对应的方法是显而易见的。
编辑:我忘记提到这个解决方案是不区分大小写的,因为在像这样的搜索中,你通常不关心大小写。如果希望它区分大小写,则删除.toUpperCase()。
https://stackoverflow.com/questions/51672223
复制相似问题