我能够从api检索数据,但是唯一的问题是,当我每次尝试从搜索中输入关键字时都尝试返回数据时,该关键字将不会显示。我希望无论输入什么关键字,该关键字都将仅显示与该关键字相关的所有数据。
但是我想,如果我输入“练习1”,那么只会显示练习1的数据。以下是我的代码:
TS
this.searchResults = this.searchService.entities$
.pipe(
map((res: Search[]) => {
return res.filter(results => {
for (const search of results.studytexts[Symbol.iterator]()) {
const getResults =
new RegExp(this.searchInput.nativeElement.value || this.inputSearchValue.keyword, 'gi').exec(search.content) ||
new RegExp(this.searchInput.nativeElement.value || this.inputSearchValue.keyword, 'gi').exec(search.name);
if (search.name.includes(getResults) || search.content.includes(getResults)) {
this.hasError = false;
console.log(search);
return search;
}
}
});
}),
HTML
<div *ngFor="let search of searchResults | async" class="search-result-item">
<div *ngFor="let result of search.studytexts">
<h1>{{ result.name }}</h1>
<p [innerHTML]="highlightSearchString(result.content)"></p>
</div>
</div>