如何在 REST 后端使用 ng2-bootstrap Typahead

How to use ng2-bootstrap Typahead with REST backend

如何在 Typescript 中使用 REST 服务作为源进行预输入?我需要使用 promise 还是可以使用 observables?具体来说,我正在从 ng2-bootstrap 主页上的示例中寻找我在 getAsyncData() 函数中需要的内容。

REST 响应:

[{id: 1, name: 'Alabama'}, {id: 2, name: 'Alaska'}, {id: 3, name: 'Arizona'},{id: 4, name: 'Arkansas'}, {id: 5, name: 'California'}]

我有什么(不起作用):

home.ts

import {Component} from 'angular2/core';
import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/common';
import {TYPEAHEAD_DIRECTIVES} from '../../../ng2-bootstrap';
import {Http,URLSearchParams,Headers} from 'angular2/http';

// webpack html imports
let template = require('./typeahead-demo.html');

@Component({
    selector: 'typeahead-demo',
    directives: [TYPEAHEAD_DIRECTIVES, CORE_DIRECTIVES, FORM_DIRECTIVES],
    template: template
})

export class TypeaheadDemo {
  public selected:string = '';
  public asyncSelected:string = '';
  public typeaheadLoading:boolean = false;
  public typeaheadNoResults:boolean = false;
  public states:Array<string> = [];
  public statesComplex:Array<any> = [];

  public getContext() {
    return this;
  }

  public _cache:any;
  public _prevContext:any;

  public constructor(public http:Http) {}

   public getAsyncData(context:any):Function {
        if (this._prevContext === context) {
            return this._cache;
        }
        this._prevContext = context;
        let f:Function = function (http:Http) {
            let searchParams = new URLSearchParams();
            searchParams.set('search', context.asyncSelected);
            http.get('http://example.com/search', {search: searchParams})
                    .subscribe(res => {
                        context.states = res.json();
                    });
        };
        this._cache = f(this.http);
        return this._cache;
  }

  public changeTypeaheadLoading(e:boolean) {
    this.typeaheadLoading = e;
  }

  public changeTypeaheadNoResults(e:boolean) {
    this.typeaheadNoResults = e;
  }

  public typeaheadOnSelect(e:any) {
    console.log(`Selected value: ${e.item}`);
  }
}

home.html

<div class='container-fluid'>
<pre>Model: {{asyncSelected | json}}</pre>
<input [(ngModel)]="asyncSelected"
       [typeahead]="getAsyncData(getContext())"
       (typeaheadLoading)="changeTypeaheadLoading($event)"
       (typeaheadNoResults)="changeTypeaheadNoResults($event)"
       (typeaheadOnSelect)="typeaheadOnSelect($event)"
       [typeaheadOptionsLimit]="7"
       placeholder="Locations loaded with timeout"
       class="form-control">
<div [hidden]="typeaheadLoading!==true">
    <i class="glyphicon glyphicon-refresh ng-hide" style=""></i>
</div>
<div [hidden]="typeaheadNoResults!==true" class="" style="">
    <i class="glyphicon glyphicon-remove"></i> No Results Found
</div>

由于 yannickadam 对 github 的回复,我能够让它工作:

home.ts

...
public autoCompleteRef = this.autoComplete.bind(this);

public autoComplete() {
    let headers = new Headers();
    headers.append('Authorization', 'Bearer ' + this.authservice.getToken());

    let searchParams = new URLSearchParams();
    searchParams.set('search', this.autoCompleteSearchTerm);

    return this.http.get(this.api_url, {search: searchParams, headers: headers})
        .map(res => res.json())
        .map((el)=> {
            return el.map((data)=> {
                return ({id: data.id, name: data.name}); // unnecessary as format is the same in this case
            });
        })
        .toPromise();
}
...

html.html

<input class="form-control"
       [(ngModel)]="autoCompleteSearchTerm"
       [typeahead]="autoCompleteRef"
       [typeaheadOptionField]="'name'"
       [typeaheadWaitMs]="300"
       [typeaheadMinLength]="1"
       (typeaheadOnSelect)="typeaheadOnSelect($event)">