如何使用REST服务作为打字稿中的来源进行预先输入?我需要使用承诺还是可以使用observable?具体来说,我正在寻找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>
答案 0 :(得分:4)
感谢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)">