【发布时间】:2022-01-12 19:12:35
【问题描述】:
我有一个搜索栏,当输入文本时会显示结果,问题是:
- 即使输入字段/搜索栏为空,搜索结果也不会消失。
- 如果我按 ESC 或在搜索栏或搜索结果外部单击,它不会关闭。我用渲染器和主机视图尝试了不同的东西,我不能让它工作。如果是普通的js,我敢肯定我会解决这个问题的。 Angular 有太多特殊的怪癖,我需要一些帮助。
这就是问题的样子:problem
components.ts 文件(删除了我失败的尝试):
import {
Component,
OnInit,
Renderer2,
ElementRef,
ViewChild,
} from '@angular/core';
import { faSearch } from '@fortawesome/free-solid-svg-icons';
import { CountryService } from '../services/country.service';
import { OneCountry } from '../country';
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css'],
})
export class SearchComponent implements OnInit {
faSearch = faSearch;
countries: OneCountry[] = [];
searchTerm: any;
cachedCountries: OneCountry[] = [];
constructor(private countryService: CountryService) {}
ngOnInit(): void {}
onKeyPressEvent(event: KeyboardEvent): void {
this.getCountries();
}
getCountries(): void {
this.countryService.searchCountries().subscribe({
next: (countries) => (
(this.countries = countries),
(this.cachedCountries = this.countries),
console.log(this.countries)
),
});
}
search(value: string): void {
this.countries = this.cachedCountries.filter((val) =>
val.name.toLowerCase().includes(value)
);
}
}
这是模板文件:
<div id="search-component" class="w-full md:w-[32rem] dark:bg-darkblue-100">
<div
class="w-full px-4 h-[53px] flex justify-around align-center shadow-md border rounded"
>
<figure class="w-1/6 grid place-items-center">
<fa-icon
class="text-darkblue-100 dark:text-white text-lg"
[icon]="faSearch"
></fa-icon>
</figure>
<input
class="w-5/6 h-full focus:outline-none dark:bg-darkblue-100"
placeholder="Search for a country..."
#searchBox
name="searchTerm"
id="search-box"
(input)="search(searchBox.value)"
[(ngModel)]="searchTerm"
(keypress)="onKeyPressEvent($event)"
/>
</div>
<ul class="mt-0 pl-0 relative z-20">
<li
class="z-20"
*ngFor="let country of countries | searchFilter: searchTerm; index as i"
>
<a
*ngIf="i < 10"
routerLink="/detail/{{ country.name }}"
class="z-20 border border-t-0 inline-block w-full md:w-[32rem] p-4 rounded shadow hover:bg-darkblue-100 hover:text-white dark:hover:bg-white dark:hover:text-black h-12 box-border"
>{{ country.name }}</a
>
</li>
</ul>
</div>
【问题讨论】:
-
好奇你为什么使用 (input) 和 (keypress) - 它们或多或少不会触发同一个事件吗?
-
@Kinglish 按键和输入只是尝试不同的事情并希望一个人坚持下来,忘记删除按键的结果。接受了答案中的建议并使用了 observables。
标签: javascript html angular typescript dom