gpt4 book ai didi

html - 仅当使用 Angular 7 关注输入字段时元素的外观

转载 作者:太空宇宙 更新时间:2023-11-04 06:32:39 25 4
gpt4 key购买 nike

有人可以建议如何仅在选择输入字段 search-form__input 后显示带有过去查询的弹出菜单 suggestions 吗?

search.component.html

<div class="search-form mt-2 input-group mb-2">
<input appShowSuggestions type="search" class="form-control search-form__input"
placeholder="Поиск (макс. количество найденных статей - 30)" maxlength="60"
[(ngModel)]="searchQuery"
(keyup)="showArticlesKeyup($event)"
[formControl]="typeahead"
(input)="suggest()" >
<div class="input- group-append">
<button class="btn btn-outline-secondary search-form__btn" type="submit"
(click)="showArticles()" [disabled]="searchQuery.trim() === ''">Искать</button>
</div>
</div>
<div class="suggestions" *ngIf="suggestions.length">
<small class="text-muted">Ваши прошлые запросы:</small>
<p class="suggestions__link"
*ngFor="let suggest of suggestions"
(click)="searchQuery = suggest; showArticles()"> {{ suggest }}</p>
</div>

search.component.ts

export class SearchComponent implements OnInit {
constructor(private articleService: ArticleService) { }

searchQuery = '';
articles: any[] = [];
searchQueryCollection: string[] = [];
suggestions: string[] = [];
typeahead: FormControl = new FormControl();


static getUrl(searchQuery: string) {
return 'https://ru.wikipedia.org/w/api.php?action=opensearch&profile=normal&search='
+ searchQuery + '&limit=30&namespace=0&format=json&origin=*';
}

suggest() {
this.suggestions = Array.from(new Set(this.searchQueryCollection))
.filter(c => c.startsWith(this.typeahead.value))
.slice(0, 5);
}

ngOnInit() {
}

showArticles() {
this.articleService.getArticles(SearchComponent.getUrl(this.searchQuery))
.subscribe(
(data: IArticle) => {
this.articles = Object.values({ ...data });
this.searchQueryCollection.push(this.searchQuery);
}
);
}

showArticlesKeyup(event) {
if (event.key === 'Enter' && this.searchQuery.trim() !== '') {
this.showArticles();
}
}
}

最佳答案

<input appShowSuggestions type="search" class="form-control search-form__input"
placeholder="Поиск (макс. количество найденных статей - 30)" maxlength="60"
[(ngModel)]="searchQuery"
(keyup)="showArticlesKeyup($event)"
[formControl]="typeahead"
(input)="suggest()" >

一个建议是添加 (focus)="toggleSuggestions()" 然后切换建议如下所示:

toggleSugestions = () => this.showSuggestions = !this.showSuggestions;

然后您可以将 ng if 更改为类似以下内容:

<div class="suggestions" *ngIf="showSuggestions && suggestions.length">

关于html - 仅当使用 Angular 7 关注输入字段时元素的外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54514568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com