我尝试使用搜索框来使用Angular 2过滤项目列表,但是当我在搜索框中输入内容时,它永远不会触发服务上的搜索方法。我知道我可能会遗漏一些简单的东西,但任何帮助都会非常感激。
company.service.ts:
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/toPromise';
import { CompanyModel } from './company';
import { CompaniesModel } from './company';
@Injectable()
export class CompanyService {
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
search(term: string): Observable<CompaniesModel> {
console.log('search service', term);
return this.http
.get('/api/Company/Search/?search=' + term)
.map((r: Response) => r.json().data as CompaniesModel);
}
}
companies.component.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { FormControl } from '@angular/forms';
import { CompanyModel } from './company';
import { CompaniesModel } from './company';
import { CompanyService } from './company.service';
@Component({
moduleId: module.id,
selector: 'companies-list',
templateUrl: 'companies.component.html'
})
export class CompaniesComponent implements OnInit {
companiesModel: Observable<CompaniesModel>;
private searchTerms = new Subject<string>();
constructor(
private companyService: CompanyService,
private router: Router) { }
search(term: string): void {
this.searchTerms.next(term);
}
ngOnInit(): void {
this.companiesModel = this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.switchMap(term => this.companyService.search(term));
}
gotoDetail(company: CompanyModel): void {
let link = ['/company', company.Id];
this.router.navigate(link);
}
}
companies.component.html
<h2>Companies</h2>
<div>
<label>Search: </label>
<input #searchBox id="search-box" (keyup)="search(searchBox.value)" />
</div>
<div>
<button (click)="create()">
Add
</button>
</div>
<div *ngIf="companiesModel" class="grid grid-pad">
<ul>
<li *ngFor="let company of companiesModel.Companies | async" (click)="gotoDetail(company)">
<div>
<h4>{{company.Name}}</h4>
</div>
</li>
</ul>
</div>
答案 0 :(得分:1)
您永远不会订阅主题searchTerms
。如果您没有订阅,那么您已经铺设的运营商将永远不会被执行。
this.searchTerms
.debounceTime(300)
.distinctUntilChanged()
.switchMap(term => this.companyService.search(term))
.subscribe((val) => //do something with the result here);
示例Jsbin。添加subscribe语句以查看控制台日志。 http://jsbin.com/zijuvet/edit?html,js,console
答案 1 :(得分:0)
您也可以使用以下代码尝试相同的
<input type="text" [(ngModel)]="queryString" id="search" placeholder="Search to type">
管
@Pipe({
name: 'FilterPipe',
})
export class FilterPipe implements PipeTransform {
transform(value: any, input: string) {
if (input) {
input = input.toLowerCase();
return value.filter(function (el: any) {
return el.toLowerCase().indexOf(input) > -1;
})
}
return value;
}
}
您可以根据自己的要求修改代码。
答案 2 :(得分:0)
Angular2搜索没有任何自定义管道
<table>
<thead>...</thead>
<tbody>
<tr *ngFor="let part of filterParts let i=index"></tr>
</tboby>
// component.ts文件中的代码
Partsfiltering(inputName){
this.filterParts = [];
if(inputName != ""){
this.parts.forEach(element => {
if(element.partNumber.toUpperCase().indexOf(inputName.toUpperCase())>=0){
this.filterParts.push(element);
}
});
}else{
this.filterParts = this.parts;
}
}