我想将搜索组件实现为可重用组件,基本上这个搜索组件会在我输入内容时调用网络服务。这样它就会在那个 url 中搜索并获取数据并显示它。我在 lib 文件夹中制作了这个组件并在 app 组件中使用它,但搜索没有发生,事实上过滤器方法没有在组件中触发。请在下面检查我的组件代码。
ngOnInit() {
console.log('input variables are ', this._appId, this._sandbox, this._typeId);
this.bpmService.login().subscribe((data) => {
console.log('=== Login Success ===');
});
this.filteredOptions = this.myControl.valueChanges
.pipe(
startWith(''),
debounceTime(10),
distinctUntilChanged(),
switchMap(val => {
return this.filter(val);
})
);
}
displayFn(user: Case): any {
return user && JSON.parse(user.summary).field1 ? JSON.parse(user.summary).field1 :
this._cases;
}
private filter(val: string): Observable<any[]> {
console.log('filter value is ', val);
return this.bpmService.getCases(this._sandbox, this._appId, this._typeId)
.pipe(
map(response => response.filter((element: { summary: string; }) => {
console.log('Response is ====', response);
return JSON.parse(element.summary).field1.match(val);
}))
);
}
这是我的 HTML
<div class="la-case-search">
<mat-label>Search within cases</mat-label>
<div class="la-case-search__input">
<input type="text" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-icon svgIcon="search"></mat-icon>
</div>
<mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn" (optionSelected) = "selectedCase($event.option.value)">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option">
{{option.summary ? option.summary:''}}
</mat-option>
</mat-autocomplete>
</mat-form-field>