Я использую NgbHighlight, чтобы позволить пользователю просматривать список.
Моя проблема с выделением результата.
Я хочу выделить только первый матч.
Я использую самые основы, описанные здесь: https://ng -bootstrap.github.io / # / components / typeahead / examples
Код: https://ng -bootstrap.github.io / stackblitzes / typeahead / http / stackblitz.html
Список: ['ABCD AAAA ABCD', 'BBBB ABCD BBBB', 'CCCC CCCC CCCC'];
Поиск: 'ABCD'
Вывод, который я хочу:
' ABCD AAAA ABCD'
'BBBB ABCD BBBB'
На выходе я получаю:
' ABCD AAAA ABCD '
'BBBB ABCD BBBB'
Что я должен сделать, чтобы ограничить подсветку первой частью?
Спасибо за помощь:)
Редактировать: простой пример
HTML
<div class="form-group">
<label for="typeahead-http">Search for a wiki page:</label>
<input id="typeahead-http" type="text" class="form-control" [class.is-invalid]="searchFailed" [(ngModel)]="model" [ngbTypeahead]="search" placeholder="Wikipedia search" />
<span *ngIf="searching">searching...</span>
<div class="invalid-feedback" *ngIf="searchFailed">Sorry, suggestions could not be loaded.</div>
</div>
машинопись
import {Component, Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import {catchError, debounceTime, distinctUntilChanged, map, tap, switchMap} from 'rxjs/operators';
const WIKI_URL = 'https://en.wikipedia.org/w/api.php';
const PARAMS = new HttpParams({
fromObject: {
action: 'opensearch',
format: 'json',
origin: '*'
}
});
@Injectable()
export class WikipediaService {
constructor(private http: HttpClient) {}
search(term: string) {
if (term === '') {
return of([]);
}
return this.http
.get(WIKI_URL, {params: PARAMS.set('search', term)}).pipe(
map(response => response[1])
);
}
}
@Component({
selector: 'ngbd-typeahead-http',
templateUrl: './typeahead-http.html',
providers: [WikipediaService],
styles: [`.form-control { width: 300px; display: inline; }`]
})
export class NgbdTypeaheadHttp {
model: any;
searching = false;
searchFailed = false;
constructor(private _service: WikipediaService) {}
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(300),
distinctUntilChanged(),
tap(() => this.searching = true),
switchMap(term =>
this._service.search(term).pipe(
tap(() => this.searchFailed = false),
catchError(() => {
this.searchFailed = true;
return of([]);
}))
),
tap(() => this.searching = false)
)
}