私はngx-bootstrap/typeaheadを使用して自分のページにオートコンプリートを作成しています。これは私が現在使用しているコードである:角度指令でngx-bootstrap/typeaheadをラップします
コンポーネント<input type="text" class="form-control" name="countryName" autocomplete="off" [(ngModel)]="asyncSelected" (blur)="typeaheadOnBlur()" [typeahead]="countryDataSource" (typeaheadOnSelect)="typeaheadOnSelect($event)" typeaheadWaitMs="300" typeaheadOptionField="name">
:
asyncSelected: string;
constructor() {
this.countryDataSource = Observable.create((observer: any) => {
observer.next(this.asyncSelected);
}).mergeMap((input: string) => this.getAutoCompleteResults(input));
}
typeaheadOnSelect(event: TypeaheadMatch): void {
viewModel.nestedProperty.country = event.item.name;
viewModel.nestedProperty.countryCode = event.item.countryCode;
}
typeaheadOnBlur(): void {
viewModel.nestedProperty.country = asyncSelected;
}
getAutoCompleteResults()
次の形式のオブジェクトの配列(観察)を返し:今
[{id: 1, name: "Australia", code: "AU"}, {id: 2, name: "United States", code: "US"}, ...]
を私のコンポーネントのコードは、オートコンプリートを使用しているコンポーネントに属していないと思います。それはそれほど再利用できません。
<input [myAutoComplete] [ngModel]="viewModel.nestedProperty?.country" (NgModelChange)="viewModel.nestedProperty.country=$event" (select)="mySelectFunction(???)" />
:私は、私は私の自動補完を使用するたびに、私は次のようにそれを使用するディレクティブを作成すると思ってコンポーネント内のすべてのこれらのコードを持つようにしたいと、これらすべての(blur)="typeaheadOnBlur()"
、typeaheadWaitMs="300"
はありませんご存じのように、viewModel.nestedProperty.country
をngx-bootstrapとのバインディングに使用できませんでした。この$event
は、typeaheadOnSelect($event)
のngx-bootstrap $event
とは異なる構造を持つように見えます。
また、(select)="mySelectFunction(???)"
の処理方法もわかりません。自分のプロジェクトでこのオートコンプリートをもっと再利用できるようにするにはどうすればよいですか?